cs95
cs95

Reputation: 402483

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 into groups such that

The idea is to find all sequences that terminate with 0 and group them together. The sequences can vary in size and and the last sequence may not terminate with 0. The first element will never be 0.

My end result looks something like this:

C_new
9
6
23

Where I find these groups and then sum them.

Upvotes: 1

Views: 64

Answers (1)

jezrael
jezrael

Reputation: 862641

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

Upvotes: 1

Related Questions