Pandas - 按多列分组的移动平均线 [英] Pandas - moving average grouped by multiple columns

查看:109
本文介绍了Pandas - 按多列分组的移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pandas 新手,请耐心等待.

New to Pandas, so bear with me.

我的数据框的格式

date,name,country,tag,cat,score
2017-05-21,X,US,free,4,0.0573
2017-05-22,X,US,free,4,0.0626
2017-05-23,X,US,free,4,0.0584
2017-05-24,X,US,free,4,0.0563
2017-05-21,X,MX,free,4,0.0537
2017-05-22,X,MX,free,4,0.0640
2017-05-23,X,MX,free,4,0.0648
2017-05-24,X,MX,free,4,0.0668

我正在尝试想出一种方法来找到国家/标签/类别组内的 X 天移动平均线,因此我需要:

I'm trying to come up with a way to find the X day moving average within the country/tag/category group, so I need:

date,name,country,tag,cat,score,moving_average
2017-05-21,X,US,free,4,0.0573,0
2017-05-22,X,US,free,4,0.0626,0.0605
2017-05-23,X,US,free,4,0.0584,0.0594
2017-05-24,X,US,free,4,0.0563,and so on
...
2017-05-21,X,MX,free,4,0.0537,and so on
2017-05-22,X,MX,free,4,0.0640,and so on
2017-05-23,X,MX,free,4,0.0648,and so on
2017-05-24,X,MX,free,4,0.0668,and so on

我尝试了按我需要的列进行分组的方法,然后使用 pd.rolling_mean 但我最终得到了一堆 NaN

I tried something on the lines of grouping by the columns I need followed by using pd.rolling_mean but I end up with a bunch of NaN's

df.groupby(['date', 'name', 'country', 'tag'])['score'].apply(pd.rolling_mean, 2, min_periods=2)  # window size 2

我将如何正确执行此操作?

How would I go about doing this properly?

推荐答案

IIUC:

(df.assign(moving_score=df.groupby(['name','country','tag'], as_index=False)[['score']]
                           .rolling(2, min_periods=2).mean().fillna(0)
                           .reset_index(0, drop=True)))

输出:

         date name country   tag  cat   score  moving_score
0  2017-05-21    X      US  free    4  0.0573       0.00000
1  2017-05-22    X      US  free    4  0.0626       0.05995
2  2017-05-23    X      US  free    4  0.0584       0.06050
3  2017-05-24    X      US  free    4  0.0563       0.05735
4  2017-05-21    X      MX  free    4  0.0537       0.00000
5  2017-05-22    X      MX  free    4  0.0640       0.05885
6  2017-05-23    X      MX  free    4  0.0648       0.06440
7  2017-05-24    X      MX  free    4  0.0668       0.06580

这篇关于Pandas - 按多列分组的移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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