Ankit Kumar
Ankit Kumar

Reputation: 349

How to get start and end index of group of 1's in a series?

I've a series like below:

index value
0       0
1       1
2       1
3       1
4       0
5       0
6       1
7       1
8       0
9       1
10      0
11      0
12      1
13      1
14      0

and the expected output is:

index, start, end
0       1       3
1       6       7
2       9       9
3       12      13

How can I achieve this with pandas?

Upvotes: 2

Views: 778

Answers (2)

ibarrond
ibarrond

Reputation: 7621

Just a couple of lines using pandas using the index of pd.Series.diff:

# Difference between consecutive values. A -1 is an 'end', a 1 is a 'start'
df_diff = df.diff()
df_res = pd.DataFrame()
df_res['start'] = df_diff[df_diff==1].index
df_res['end'] = df_diff[df_diff==-1].index -1

# Result
df_res
#   start  end
#0      1    3
#1      6    7
#2      9    9
#3     12   13

Note: df generated using:

import pandas as pd
df = pd.Series({0:0,1:1,2:1,3:1,4:0,5:0,6:1,7:1,8:0,9:1,10:0,11:0,12:1,13:1,14:0})

Upvotes: 0

jezrael
jezrael

Reputation: 863166

Create consecutive 1 groups by Series.shift with Series.cumsum and filtering with Series.eq (==) and then aggregate GroupBy.first and GroupBy.last:

df = df.reset_index()
m = df['value'].eq(1)
g = m.ne(m.shift()).cumsum()[m]

df = df.groupby(g)['index'].agg([('start','first'),('end','last')]).reset_index(drop=True)
print (df)
   start  end
0      1    3
1      6    7
2      9    9
3     12   13

Upvotes: 3

Related Questions