【问题标题】:How can I compute the cumulative points?如何计算累积点数?
【发布时间】:2020-05-11 11:49:33
【问题描述】:

我有以下数据框:

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

结果:
"HT" = 主队获胜 --> 主队 +3,客队 0
"AT" = 客队获胜 --> 主队 0,客队 +3
"D" = 平局 --> 主队 +1,客队 +1

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

我正在使用Python,但我的循环运行不完美。


这是我的预期结果:

【问题讨论】:

  • 你能分享你的代码,还有你的预期输出。
  • 是否要将当前比赛计入累计积分?
  • 我的循环不能正常工作 - 你能在问题中包含这个循环吗?
  • 例如,如果主队是 A,是否要包括 A 的积分直到那场比赛?或者您只是想知道在该日期之前您在家中获得了多少积分,而不管球队如何?你能显示预期的输出吗?对于这个 DataFrame
  • 我刚刚用预期的结果更新了我的答案,谢谢!

标签: python pandas loops dataframe


【解决方案1】:

完全使用 Pandas 的无循环和灵活解决方案

使用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 

【讨论】:

    【解决方案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
    

    【讨论】:

    • 感谢您的帮助,但最终结果与预期不符。问题是主队和客队每场比赛都会改变
    • 检查编辑的答案,如果您有任何疑问,请随时发表评论。
    • 如果有帮助,请接受答案。它可以帮助整个社区识别正确的答案。您可以通过单击答案旁边的勾号来做到这一点。干杯。
    猜你喜欢
    • 2015-09-07
    • 2013-01-26
    • 1970-01-01
    • 2021-05-11
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多