2017-04-25 65 views
0

我正在使用网球数据集并希望将其转换为增强信息。我一直在寻找解决我的问题,但还没有发现任何东西。在网球数据框中创建具有平均统计值的列

我想创建一列,显示每行玩家以前匹配的平均统计值。如你所知,在每一行中,对于玩家1应该有一些值,对于玩家2来说应该有一些值,以及对于先前游戏的平均统计指标。此外,当球员在前一场比赛中是第一名或第二名时,需要取值。

下面是数据集的一部分:

Player 1  Player 2  P1_Prct1stIn P2_Prct1stIn P1_Prct1stWon P2_Prct1stWon 
1 Djokovic N. Raonic M.  0.644444  0.576923   0.724138 0.844444 
2 Cilic M.  Wawrinka S. 0.450000  0.610390   0.861111 0.787234 
3 Nishikori K. Murray A.  0.630252  0.530303   0.720000 0.757143 
4 Raonic M.  Thiem D.  0.637931  0.545455   0.864865 0.6666671 
5 Djokovic N. Goffin D.  0.614035  0.468085   0.828571 0.590909 
6 Wawrinka S. Murray A.  0.611940  0.588235   0.658537 0.866667 
7 Nishikori K. Cilic M.  0.635135  0.545455   0.638298 0.809524 
8 Raonic M.  Murray A.  0.632812  0.609589   0.740741 0.617978 
9 Nishikori K. Djokovic N. 0.636364  0.636364   0.514286 0.821429 
10 Murray A. Djokovic N. 0.542373  0.721311   0.843750 0.590909 

Prct1stIn - 1的比例发球

Prct1stWon - 1的比例成为拉力赛中荣获

所以我需要的列使用此数据集制作像这样:

P1_PREV_MEAN_Prct1stIn 
NaN 
NaN 
NaN 
NaN 
0.644444 
0.610390 
0.630252 
0.607427 
0.6326935 
0.57604233 

很好,如果一个人会显示m以及如何取平均值,例如,还有玩家的最后10场比赛。

我将不胜感激任何帮助决定这个问题。

回答

0

首先我想创建一个有用的pd.MultiIndex

mux = pd.MultiIndex.from_tuples([ 
     ('P1', 'Name'), ('P2', 'Name'), 
     ('P1', 'Prct1stIn'), ('P2', 'Prct1stIn'), 
     ('P1', 'Prct1stWon'), ('P2', 'Prct1stWon') 
    ]) 

d1 = pd.DataFrame(df.values, df.index, mux).sort_index(1) 
print(d1) 

enter image description here


现在我倒是stackgroupby'Name'applyexpandingmean这一直是shift倒是

f = lambda x: x.expanding().mean().shift() 
g = d1.stack(0).groupby('Name')[['Prct1stIn', 'Prct1stWon']] 
d2 = g.apply(f).unstack().swaplevel(0, 1, 1) 
d1.join(d2, rsuffix=' Prior Mean') 

enter image description here

+0

谢谢你的回答!不幸的是,这在处理整个数据集时不起作用(这里我只展示了它的一部分)。 – Maxim

+0

你打算展示更多吗?还是分享出了什么问题? – piRSquared

+0

是的,没问题!这里是链接观看它:https://github.com/maanisimov/Tennis-matches-analysis/blob/master/Analysis%20of%20Tennis%20Bets%20Formation%20in%20the%20BM.ipynb。 有链接到原始信息的网站以及。 我想俄语不会让你感到困惑,因为要监视数据帧。但如果这是一个问题,我可以制作一个英文版本。 – Maxim