滚动条件均值 [英] Conditional mean by rolling

查看:60
本文介绍了滚动条件均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

                  Data       flag
2017-01-01        17.2        False     
2017-01-02        17.0        False      
2017-01-03        16.8        False   
2017-01-04        18.3        False      
2017-01-05        19.1        True       
...
2017-12-28        20.1        False      
2017-12-29        19.8        False      
2017-12-30        18.9        False      
2017-12-31        19.5        False      

有一个具有值和标志的熊猫数据框.如果标志为"NOT TRUE",我想通过滚动(window = 30)计算平均值.

There is a pandas dataframe that has values and flag. I want to calculate mean values by rolling(window=30), if the flag is "NOT TRUE".

推荐答案

您可以使用

You can use pandas.rolling_mean() while subsetting the dataframe to only include entries where df.flag is false (the ~ operator inverts the truth of the boolean series, getting all values where df.flag is False).

pandas.rolling_mean(df[~df.flag], window=30)

这篇关于滚动条件均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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