如何在 pandas MultiIndex数据框中有条件地重置滚动最大值的初始值/行? [英] How to conditionally reset a rolling max's initial value/row in pandas MultiIndex dataframe?

查看:83
本文介绍了如何在 pandas MultiIndex数据框中有条件地重置滚动最大值的初始值/行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多索引数据框.索引列是DateSymbol.我想重置数据帧开始为每个Symbol评估numberrolling_max的行.我想基于包含TrueFalse的列来执行此操作.如果Date上的conditionTrue,则应重置rolling_max并从该Date计算最大值.如果conditionFalse,则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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆