user2988577
user2988577

Reputation: 4207

Groupby consecutive elements in pandas series with same key

I have a Series in DataFrame which contains only 0 or 1. I want to get the list of lengths of each 'cluster' of 0. Here is a toy model:

a=pd.DataFrame({'x':[1,1,0,0,1,0]})

What I expect is [2,1]

To do that I could the following:

a[a['x']==0].index

and then use groupby for looking for consecutive indexes as already learned on SO. By the way is there a more efficient way to do that?

Upvotes: 1

Views: 493

Answers (1)

hughdbrown
hughdbrown

Reputation: 49063

You can get this result using itertools.groupby.

from itertools import groupby

data = [1, 1, 0, 0, 1, 0]
grouper = (list(s) for _, s in groupby(data))
lengths = [len(s) for s in grouper if s[0] == 0]
print(lengths)

Not sure if groupby in pandas is coded the same.

Upvotes: 1

Related Questions