Skip to content Skip to sidebar Skip to footer

Custom Groupby Based On Column Values

Given this dataframe: C index  0   9 1   0 2   1 3   5 4   0 5 1 6 2 7 20 8 0 How can I split this int

Solution 1:

Use groupby by Series:

print (df['C'].shift(1).eq(0).cumsum())
0    0
1    0
2    1
3    1
4    1
5    2
6    2
7    2
8    2
Name: C, dtype: int32

df = df['C'].groupby(df['C'].shift(1).eq(0).cumsum()).sum()
print (df)
C
0     9
1     6
2    23
Name: C, dtype: int64

Post a Comment for "Custom Groupby Based On Column Values"