I have a pandas dataframe with the format:
| Condition | Group | |
|---|---|---|
| 1 | True | 0 |
| 2 | False | 0 |
| 3 | True | 0 |
| 4 | True | 0 |
| ... | ... | ... |
I want to update it such that the group number is one plus the previous group number only if the condition is true:
| Condition | Group | |
|---|---|---|
| 1 | True | 0 |
| 2 | False | 0 |
| 3 | True | 1 |
| 4 | True | 2 |
| ... | ... | ... |
What is the most pythonic way to do this? Is using a loop easiest, or is there a more efficient or readable way with pandas?
CodePudding user response:
This is cumsum:
s = df['Condition'].cumsum()
df['Group'] = s - s.min()
