同时计算2列的扩展均值

时间:2014-01-26 08:39:50

标签: python pandas mean

我有一张由2名球员互相竞争的桌子:

    date         plA    plB    ptsA ptsB
0   01/01/2013  Jeff    Tom     78  72
1   15/01/2013  Jeff    Tom     52  67
2   01/02/2013  Tom     Jeff    91  93
3   15/02/2013  Jeff    Tom     83  87
4   01/03/2013  Tom     Jeff    65  76

我想应用展开式平均值,以便每个玩家的ptsAptsB计入(并且不会留给)净结果。最终输出应该更清楚:

    date         plA    plB    ptsA ptsB   meanA  meanB 
0   01/01/2013  Jeff    Tom     78  72      78     72      # init mean
1   15/01/2013  Jeff    Tom     52  67      65     69.5 
2   01/02/2013  Tom     Jeff    91  93      74.3   76.6    # Tom: (72+67+91)/3, Jeff: (78+52+93)/3
3   15/02/2013  Jeff    Tom     83  87      76.5   79.25   # Jeff: (78+52+93+83)/4, Tom: (72+67+91+87)/4
4   01/03/2013  Tom     Jeff    65  76      76.4   76.4    # Tom: (72+67+91+87+65)/5, Jeff: (78+52+93+83+76)/5

现在,我开始按plA对数据进行分组,如下所示:

by_A = players.sort(columns='date').groupby('plA')
players['meanA'] = by_A['ptsA'].apply(pd.expanding_mean)
players['meanB'] = by_A['ptsB'].apply(pd.expanding_mean)

显然,我需要做同样的事情,groupby('plB')然后我正在绘制一个空白如何正确加入这两个结果。

也许熊猫提供了内置功能或者您有解决方案吗?

@EDIT Saullo Castro的解决方案,数据略有不同

    date    studentA    studentB    scoreA  scoreB  meanJeff    meanTom     meanMaggie
0   2013-01-01  Jeff    Tom     78  72             78.000000    72.000000   0.000000
1   2013-01-15  Jeff    Maggie  52  67             65.000000    36.000000   33.500000
2   2013-02-01  Tom     Jeff    91  93             74.333333    54.333333   22.333333
3   2013-02-15  Jeff    Tom     83  87             76.500000    62.500000   16.750000
4   2013-03-01  Tom     Jeff    65  76             76.400000    63.000000   13.400000

Maggie的意思应该一直保持67

1 个答案:

答案 0 :(得分:1)

(请参阅下面的固定解决方案)

一种方法是首先找出所有玩家的名字:

names = pd.concat((df.plA, df.plB)).unique()

然后创建一个新列,每个玩家的扩展均值:

for name in names:
    df['mean'+name] = pd.expanding_mean(df.ptsA*(df.plA==name) + df.ptsB*(df.plB==name))

导致:

                  date   plA   plB  ptsA  ptsB   meanJeff    meanTom
0  2013-01-01 00:00:00  Jeff   Tom    78    72  78.000000  72.000000
1           15/01/2013  Jeff   Tom    52    67  65.000000  69.500000
2  2013-01-02 00:00:00   Tom  Jeff    91    93  74.333333  76.666667
3           15/02/2013  Jeff   Tom    83    87  76.500000  79.250000
4  2013-01-03 00:00:00   Tom  Jeff    65    76  76.400000  76.400000

编辑:修复解决方案:

对于两个以上的名称,您可以使用以下方法构建扩展均值的公式:

df = pd.read_excel('stack.xlsx', 'tabelle1')
names = pd.concat((df.plA, df.plB)).unique()
for name in names:
    nA = df.plA==name
    nB = df.plB==name
    df['mean'+name] = np.cumsum(df.ptsA*nA + df.ptsB*nB)/np.maximum(1.,
                                     np.cumsum(1.*np.logical_or(nA, nB)))

导致:

date   plA     plB  ptsA  ptsB   meanJeff    meanTom  meanMaggie
0 2013-01-01 00:00:00  Jeff     Tom    78    72  78.000000  72.000000     0
1 2013-01-15 00:00:00  Jeff  Maggie    52    67  65.000000  72.000000    67
2 2013-02-01 00:00:00   Tom    Jeff    91    93  74.333333  81.500000    67
3 2013-02-15 00:00:00  Jeff     Tom    83    87  76.500000  83.333333    67
4 2013-03-01 00:00:00   Tom    Jeff    65    76  76.400000  78.750000    67