如何计算累积点?

时间:2020-01-25 10:49:52

标签: python pandas loops dataframe

我有以下数据框:

HomeTeam = ["A", "B", "B", "D", "C", "A", "C", "D"]
AwayTeam = ["C", "D", "A", "C", "B", "D", "A", "B"]
Result = ["HT", "AT", "HT", "HT", "D", "AT", "D", "AT"]
Round = [1,1,2,2,3,3,4,4]

dict = {'HomeTeam': HomeTeam, 'AwayTeam': AwayTeam, 'Result': Result, 'Round': Round}  

df = pd.DataFrame(dict) 

df

enter image description here

结果在哪里:
“ HT” =赢得HomeTeam-> HomeTeam + 3,AwayTeam 0
“ AT” =赢得AwayTeam-> HomeTeam 0,AwayTeam +3
“ D” =平局-> HomeTeam +1,AwayTeam +1

我需要创建两个不同的列:
1)累计积分主队:它包含从该主队获得的直到该比赛的总分。
2)客队累积得分:它包含了直到该比赛为止客队获得的总积分。

我正在使用Python,但是我的循环效果并不理想。


这是我的预期结果:

enter image description here

2 个答案:

答案 0 :(得分:2)

无循环的解决方案,仅使用熊猫即可灵活

DataFrame.meltnp.select一起使用(以获取积分)和DataFrame.pivot_table 将框架恢复为原始形状:

df = df.join(df.reset_index()
               .melt(['index','Round','Result'],value_name = 'Team',var_name = 'H/A')
               .sort_values('index')
               .assign(Points = lambda x:np.select([ x['Result'].eq('D'),
                                                     x['H/A'].eq('HomeTeam')
                                                             .mul(x['Result'].eq('HT'))|
                                                     x['H/A'].eq('AwayTeam')
                                                             .mul(x['Result'].eq('AT'))],
                                                    [1,3],
                                                    default = 0))
               .assign(CumPoints = lambda x: x.groupby('Team')
                                              .Points
                                              .cumsum()
                                              .groupby(x['Team'])
                                              .shift(fill_value = 0))
               .pivot_table(index = 'index',
                            columns = 'H/A',
                            values = 'CumPoints'
                            fill_value = 0)
               .sort_index(axis = 1,ascending = False)
               .add_prefix('CumulativePoints')

            )
print(df)

输出

  HomeTeam AwayTeam Result  Round  CumulativePointsHomeTeam  CumulativePointsAwayTeam
0        A        C     HT      1                         0                         0 
1        B        D     AT      1                         0                         0 
2        B        A     HT      2                         0                         3 
3        D        C     HT      2                         3                         0 
4        C        B      D      3                         0                         3 
5        A        D     AT      3                         3                         6 
6        C        A      D      4                         1                         3 
7        D        B     AT      4                         9                         4 

答案 1 :(得分:1)

将此添加到您的代码中:

cpts ={'A':0,'B':0,'C':0,'D':0}

cpts_ht = []
cpts_at = []
for i in range(len(df.Result)):
    cpts_ht.append(cpts[df.HomeTeam[i]])
    cpts_at.append(cpts[df.AwayTeam[i]])

    if df.Result[i]=='HT':
        cpts[df.HomeTeam[i]]+=3
    elif df.Result[i]=='AT':
        cpts[df.AwayTeam[i]]+=3
    else:
        cpts[df.HomeTeam[i]]+=1
        cpts[df.AwayTeam[i]]+=1

df['cummulative_home'] = cpts_ht
df['cummulative_away'] = cpts_at

print(df)

输出:

  HomeTeam AwayTeam Result  Round  cummulative_home  cummulative_away
0        A        C     HT      1                 0                 0
1        B        D     AT      1                 0                 0
2        B        A     HT      2                 0                 3
3        D        C     HT      2                 3                 0
4        C        B      D      3                 0                 3
5        A        D     AT      3                 3                 6
6        C        A      D      4                 1                 3
7        D        B     AT      4                 9                 4
相关问题