如何有条件地重置 pandas 数据框中的滚动最大初始值/行?

mma*_*onn 3 max dataframe pandas

我想重置数据帧开始评估的行rolling maxTrue我想基于包含or的列来执行此操作False。这是预期的输出:

     number     condition     rolling_max
0    77         False         77
1    80         False         80
2    54         True          54 # starting max calculation from this point
3    23         False         54
4    60         False         60
5    100        False         100
6    15         False         100
7    119        False         119
8    10         True          10 # starting max calculation from this point
9    65         False         65
10   20         True          20 # starting max calculation from this point
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

WeN*_*Ben 6

使用cumsum创建groupby密钥然后使用cummax

df.groupby(df.condition.cumsum()).number.cummax()
Out[889]: 
0      77
1      80
2      54
3      54
4      60
5     100
6     100
7     119
8      10
9      65
10     20
Name: number, dtype: int64
Run Code Online (Sandbox Code Playgroud)