如何计算累积分数?

2024-06-09 08:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据框:

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”=主队获胜-->;主队+3,AwayTeam 0
“AT”=AwayTeam赢得-->;主队0,AwayTeam+3
“D”=平局-->;主队+1,AwayTeam+1

我需要创建两个不同的列:
1) 主队累积积分:包含主队在该场比赛之前获得的总积分
2) 客队累积积分:它包含在该场比赛之前从客队获得的总积分

我正在使用Python,但是我的循环工作得并不完美


这是我的预期结果:

enter image description here


Tags: 数据gtdataframedfresultdictatpd
2条回答

将此添加到您的代码中:

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

解决方案不带回路,仅带熊猫即可灵活使用

使用^{}^{}(获取点)以及^{} 要将帧返回到原始sape,请执行以下操作:

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 

相关问题 更多 >