Conditional mean by rolling

不羁岁月 提交于 2021-01-27 13:40:39

问题


                  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      

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".


回答1:


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)


来源:https://stackoverflow.com/questions/42005667/conditional-mean-by-rolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!