如何在 pandas MultiIndex数据框中有条件地重置滚动最大值的初始值/行? [英] How to conditionally reset a rolling max's initial value/row in pandas MultiIndex dataframe?
问题描述
我有一个多索引数据框.索引列是Date
和Symbol
.我想重置数据帧开始为每个Symbol
评估number
的rolling_max
的行.我想基于包含True
或False
的列来执行此操作.如果Date
上的condition
为True
,则应重置rolling_max
并从该Date
计算最大值.如果condition
是False
,则rolling_max
应该正常"工作-为给定的Symbol
取今天和昨天的number
的最大值. condition
列与number
列无关(它们彼此不依赖).这是预期的输出:
I have a multiindex dataframe. Index columns are Date
and Symbol
. I want to reset the row where the dataframe starts to evaluate rolling_max
of number
for each Symbol
. I want to do this based on a column containing True
or False
. If condition
is True
on a Date
then rolling_max
should be reset and calculate max from this Date
. If condition
is False
then rolling_max
should work 'normally' - taking the max of today's and yesterday's number
for the given Symbol
. The condition
column has nothing to do with the number
column (they do not depend on each other). This is the expected output:
number condition rolling_max
Date Symbol
1990-01-01 A 29 False 29
1990-01-01 B 7 False 7
1990-01-02 A 13 True 13 # Reset rolling max for 'A'
1990-01-02 B 2 False 7
1990-01-03 A 11 False 13
1990-01-03 B 52 True 52 # Reset rolling max for 'B'
1990-01-04 A 30 False 30
1990-01-04 B 1 False 52
1990-01-05 A 19 True 19 # Reset rolling max for 'A'
1990-01-05 B 65 False 65
1990-01-06 A 17 False 19
1990-01-06 B 20 True 20 # Reset rolling max for 'B'
我该怎么做?
推荐答案
我能够解决这个问题.
df['rolling_max'] = df.groupby(['Symbol',df.groupby('Symbol')['condition'].cumsum()])['number'].cummax()
这篇关于如何在 pandas MultiIndex数据框中有条件地重置滚动最大值的初始值/行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!