Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16506

Filling a new column with cummax and cummin values with condition on a rolling interval

$
0
0

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:

ConditionValueCummaxCummin
A-1-1-1
22-1
33-1
B13-1
A111
111
-11-1
B11-1
3
A222
121
B-22-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.


Viewing all articles
Browse latest Browse all 16506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>