简体   繁体   English

熊猫将帧的移动平均值附加到帧

[英]Pandas appending moving averages of frame to frame

I can take a SMA of a column and then append it to the dataframe without a problem我可以获取一列的 SMA,然后将其附加到数据帧中而不会出现问题

BUT then I tried to take a SMA of the column values that met a certain criteria (a subset of all the rows, in my case a sma of goals scored df.f in the last averaging_period homegames df[df.ha=='H'] )但后来我试图采取了符合一定条件的列值的SMA(所有行的一个子集,在我的情况,在过去averaging_period homegames DF [df.ha == 1 H打进df.f目标的一个SMA '] )

and I get我得到

ValueError: Length of values does not match length of index

My code is wrong, I am taking the moving average of the homegames like I want, but it is removing the row when it gets to an away game, not skipping them in the averaging:我的代码是错误的,我正在按照我想要的方式计算主场比赛的移动平均值,但是当它进入客场比赛时会删除该行,而不是在平均时跳过它们:

sma = df[df.ha=='H'].f.rolling(window=averaging_period).mean()

df['f_sma%s' % averaging_period] = sma.array

Here are the lengths of the things I am trying to append together这是我试图附加在一起的东西的长度

len(df)
   Out[3]: 12938
len(sma.array)
   Out[4]: 6458
len(df[df.ha=='H'])
   Out[5]: 6458

Anyone know how I can get the sma function to not average the goals of the away game, but still retain a row that is the value of the sma up to that point?任何人都知道我怎样才能让 sma 函数不平均客场比赛的目标,但仍然保留一行是 sma 的价值到那时? Therebye returning an array the same length as my dataframe?从而返回与我的数据帧长度相同的数组?

You can use .loc filtering when assigning the rolling values to the dataframe将滚动值分配给数据时,您可以使用.loc过滤

import pandas as pd
import numpy as np

t = pd.DataFrame({'G':['a','a','h','a','h','a','a','h'],'scores':[3,2,1,5,3,1,6,5]})

t['avg'] = np.nan

t.loc[t.G=='h','avg'] = t[t.G=='h'].scores.rolling(window=2).mean()

Output:输出:

   G  scores  avg
0  a       3  NaN
1  a       2  NaN
2  h       1  NaN
3  a       5  NaN
4  h       3  2.0
5  a       1  NaN
6  a       6  NaN
7  h       5  4.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM