Skip to content Skip to sidebar Skip to footer

Count Groups Of Consecutive 1s In Pandas

I have a list of '1's and '0s' and I would like to calculate the number of groups of consecutive '1's. mylist = [0,0,1,1,0,1,1,1,1,0,1,0] Doing it by hand gives us 3 groups but i

Solution 1:

Here I count whenever there is a jump from 0 to 1. Prepending the 0 prevents not counting a leading sequence.

import numpy as np

mylist_arr = np.array([0] + [0,0,1,1,0,1,1,1,1,0,1,0])
diff = np.diff(mylist_arr)
count = np.sum(diff == 1)

Solution 2:

Option 1

With pandas. First, initialise a dataframe:

In[78]: dfOut[78]: 
    Col100102131405161718190101110

Now calculate sum total by number of groups:

In [79]: df.sum() / df.diff().eq(1).cumsum().max()
Out[79]: 
Col1    2.333333
dtype: float64

If you want just the number of groups, df.diff().eq(1).cumsum().max() is enough.


Option 2

With itertools.groupby:

In [88]: sum(array) /sum(1 if sum(g) else0for  _, g in  itertools.groupby(array))
Out[88]: 2.3333333333333335

If you want just the number of groups, sum(1 if sum(g) else 0 for _, g in itertools.groupby(array)) is enough.

Solution 3:

you can try this

import numpy as np
import pandas as pd
df=pd.DataFrame(data = [0,0,1,1,0,1,1,1,1,0,1,0])
df['Gid']=df[0].diff().eq(1).cumsum()
df=df[df[0].eq(1)]
df.groupby('Gid').size()
Out[245]: 
Gid
122431
dtype: int64

sum(df.groupby('Gid').size())/len(df.groupby('Gid').size())
Out[244]: 2.3333333333333335

Solution 4:

Here's one solution:

durations = []

for n, d inenumerate(mylist):
    if (n == 0and d == 1) or (n > 0and mylist[n-1] == 0and d == 1):
        durations.append(1)
    elif d == 1:
        durations[-1] += 1defmean(x):
    returnsum(x)/len(x)

print(durations)
print(mean(durations))

Solution 5:

You can try this:

mylist = [0,0,1,1,0,1,1,1,1,0,1,0]
previous = mylist[0]
count = 0for i in mylist[1:]:
   ifi== 1:
       ifprevious== 0:
            previous = 1else:
       ifi== 0:
            ifprevious== 1:
                 count += 1
                 previous = 0

print count

Output:

3

Post a Comment for "Count Groups Of Consecutive 1s In Pandas"