如何使用需要唯一的列在熊猫中执行移动平均?

时间:2019-01-14 06:27:27

标签: python pandas dataframe moving-average

我有一个数据框,如下所示:

          index       Player      Team      Matchup   Game_Date WL   Min   PTS   FGM   FGA   FG%  3PM  3PA   3P%  FTM   FTA   FT%  OREB  DREB   REB   AST  STL  BLK  TOV    PF  Plus_Minus  Triple_Double  Double_Double    FPT   2PA   2PM         2P% Home_Away
276100      1           John Long  TOR    TOR @ BOS  04/20/1997  W   6.0   0.0   0.0   3.0   0.0  0.0  1.0   0.0  0.0   0.0     0   0.0   0.0   0.0   1.0  0.0  0.0  0.0   0.0         2.0            0.0            0.0   1.50   2.0   0.0    0.000000      Away
276101      2       Walt Williams  TOR    TOR @ BOS  04/20/1997  W  29.0   7.0   3.0   9.0  33.3  1.0  2.0  50.0  0.0   0.0     0   3.0   3.0   3.0   2.0  2.0  1.0  1.0   3.0        20.0            0.0            0.0  19.75   7.0   2.0   28.571429      Away
276102      3            Todd Day  BOS  BOS vs. TOR  04/20/1997  L  36.0  22.0   8.0  17.0  47.1  4.0  8.0  50.0  2.0   2.0   100   8.0   8.0   6.0   4.0  0.0  0.0  3.0   8.0       -21.0            0.0            0.0  36.00   9.0   4.0   44.444444      Home
276103      4       Doug Christie  TOR    TOR @ BOS  04/20/1997  W  39.0  27.0   8.0  19.0  42.1  3.0  9.0  33.3  8.0   8.0   100   8.0   8.0   1.0   5.0  3.0  1.0  0.0   8.0        30.0            0.0            0.0  45.25  10.0   5.0   50.000000      Away
276104      5         Brett Szabo  BOS  BOS vs. TOR  04/20/1997  L  25.0   5.0   1.0   4.0  25.0  0.0  0.0     0  3.0   4.0  75.0   1.0   1.0   3.0   1.0  0.0  0.0  0.0   1.0       -11.0            0.0            0.0  10.25   4.0   1.0   25.000000      Home

我想添加一个新列,该列采用每个旧列并给出其x天移动平均值。但是,我想要每个唯一的人的移动平均线。例如,约翰·朗(John Long)可以在一个独特的日期玩几百场游戏。我希望他的移动平均值仅反映他的表现。我已经看过了熊猫的df.rolling()函数,但我不知道如何制作它,因此它分别针对每个玩家。任何帮助将不胜感激。

          Name    Date  Points  MA
0    Joe Smith  1-1-19      10  NA
1  Sam Simmons  1-1-19      20  NA
2    Joe Smith  1-2-19      30  20
3  Sam Simmons  1-2-19      40  30

2 个答案:

答案 0 :(得分:0)

您可以将groupbyrollingmean结合使用,然后在join之前添加新列:

df['Date'] = pd.to_datetime(df['Date'], format='%m-%d-%y')

s = df.set_index('Date').groupby('Name')['Points'].rolling(2, freq='D').mean().rename('MA')

df = df.join(s, on=['Name','Date'])
print (df)
          Name       Date  Points    MA
0    Joe Smith 2019-01-01      10   NaN
1  Sam Simmons 2019-01-01      20   NaN
2    Joe Smith 2019-01-02      30  20.0
3  Sam Simmons 2019-01-02      40  30.0

答案 1 :(得分:0)

从上面@jezrael的答案以及对另一个问题here的答案中汲取灵感,这是按玩家运行平均值的解决方案-不受日期窗口大小限制。

# Get the running count of Names, sorted by Date, Name
df['NameCount'] = df.sort_values(['Date','Name'], ascending=True).groupby('Name').cumcount() + 1
# Running sum of points, in the same order as above (important)
df['PointSum'] = df.sort_values(['Name','NameCount'], ascending=True).groupby('Name')['Points'].cumsum()
df['MA'] = df['PointSum']/df['NameCount']
# Drop the unneeded columns
df = df.drop(['NameCount', 'PointSum'], axis=1)
@MaxU here提供的

cumcount()方法,作为对SQL行号的仿真,按方法进行分区