gpt4 book ai didi

python - Pandas - 按多列分组的移动平均线

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:14 24 4
gpt4 key购买 nike

Pandas 新手,请多多包涵。

我的数据框是这样的格式

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 日移动平均线,因此我需要:

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

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

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

最佳答案

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

关于python - Pandas - 按多列分组的移动平均线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46351322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com