I want to count all step of each user after 'Start' event in Pandas.
My dataset:
ID | Event |
---|---|
1 | Start |
1 | Event1 |
1 | Start |
1 | Event1 |
1 | Event2 |
1 | Start |
2 | Start |
2 | Event1 |
2 | Start |
I want:
ID | Event | Count |
---|---|---|
1 | Start | 1 |
1 | Event1 | 2 |
1 | Start | 1 |
1 | Event1 | 2 |
1 | Event2 | 3 |
1 | Start | 1 |
2 | Start | 1 |
2 | Event1 | 2 |
2 | Start | 1 |
CodePudding user response:
Use groupby
cumsum
and then cumcount
:
df['Count'] = df.groupby(df['Event'].eq('Start').cumsum()).cumcount().add(1)
Output:
>>> df
ID Event Count
0 1 Start 1
1 1 Event1 2
2 1 Start 1
3 1 Event1 2
4 1 Event2 3
5 1 Start 1
6 2 Start 1
7 2 Event1 2
8 2 Start 1