I have a data frame in which I want to populate two columns of rolling cumulative max and cumulative min values for intervals between 2 conditions:
Example: I want to find cumulative max and min values for rows between A and B in the Column "Condition". If the row is outside this condition, we ignore it.
Example of desired output:
| Condition | Value | Cummax | Cummin |
|---|---|---|---|
| A | -1 | -1 | -1 |
| 2 | 2 | -1 | |
| 3 | 3 | -1 | |
| B | 1 | 3 | -1 |
| A | 1 | 1 | 1 |
| 1 | 1 | 1 | |
| -1 | 1 | -1 | |
| B | 1 | 1 | -1 |
| 3 | |||
| A | 2 | 2 | 2 |
| 1 | 2 | 1 | |
| B | -2 | 2 | -2 |
I have tried using:
df.loc[(df['Condition'].ffill()=="A")|(df['Condition']=="B"), "Cummax"] = df["Value"].cummax()However, this will grab the overall cumulative maximum value up to that point. Appreciate any advice on resolving this.