Praneeth Bhandary
Praneeth Bhandary

Reputation: 45

find first consecutive values groups by ids

I have a dataframe where I want to group first consecutive for each id

id   value 
a1   1
a1   3
a1   4
a2   1
a2   2 
a2   3
a2   5
a2   8
a2   9

I want an out put where it identifies "first" consecutive group for each id , I have tried difference measures but only in vain as it returns id values which are consecutive after a break.

I want an output of this kind

id  value
a1  1
a2  1
a2  2
a2  3

would appreciate suggestions

Upvotes: 1

Views: 59

Answers (1)

jezrael
jezrael

Reputation: 863166

Idea is create consecutive groups by compare difference for not equal q with cumulative sum and then for first groups test first values by GroupBy.transform with GroupBy.first or GroupBy.min:

s = df['value'].diff().ne(1).cumsum()
df1 = df[s.groupby(df['id']).transform('first').eq(s)]
#alternative
#df1 = df[s.groupby(df['id']).transform('min').eq(s)]
print (df1)
   id  value
0  a1      1
3  a2      1
4  a2      2
5  a2      3

Upvotes: 3

Related Questions