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

问题描述 投票:2回答:1

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

     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

我怎样才能做到这一点?

pandas dataframe max
1个回答
3
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.