跨多列滚动平均匹配案例 [英] Rolling average matching cases across multiple columns

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

问题描述

如果有人问过这个问题,我很抱歉,但我找不到其他类似的问题.

I'm sorry if this has been asked but I can't find another question like this.

我在 Pandas 中有一个这样的数据框:

I have a data frame in Pandas like this:

Home   Away   Home_Score   Away_Score
MIL    NYC    1            2
ATL    NYC    1            3
NYC    PHX    2            1
HOU    NYC    1            6

我想计算每支球队的移动平均数,但问题是我想为他们的所有比赛(包括主客场比赛)计算.

I want to calculate the moving average for each team, but the catch is that I want to do it for all of their games, both home and away combined.

因此,对于NYC"大小为 3 的移动平均窗口,第 1 行的答案应该是 (2+3+2)/3,然后第 2 行的答案应该是 (3+2+6)/3,等等.

So for a moving average window of size 3 for 'NYC' the answer should be (2+3+2)/3 for row 1 and then (3+2+6)/3 for row 2, etc.

推荐答案

你可以用exploid stack 把两列转换成一列和groupby:

You can exploid stack to convert the two columns into one and groupby:

(df[['Home_Score','Away_Score']]
   .stack()
   .groupby(df[['Home','Away']].stack().values)
   .rolling(3).mean()
   .reset_index(level=0, drop=True)
   .unstack()
   .add_prefix('Avg_')
)

输出:

   Avg_Away_Score  Avg_Home_Score
0             NaN             NaN
1             NaN             NaN
2             NaN        2.333333
3        3.666667             NaN

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

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