'Cumulative count between two specific events in Pandas
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 |
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
