gpt4 book ai didi

python - Pandas 2018 世界杯数据集净胜球

转载 作者:行者123 更新时间:2023-11-30 22:11:09 25 4
gpt4 key购买 nike

我有来自 kaggle 的 2018 年世界杯数据集。 World Cup Fifa statistics

在此数据集中,每一行代表的不是比赛,而是球队及其在比赛中的得分。

例如:

World Cup, fifa-2018-match-statistics

如何从此数据集中计算每个团队的净胜球数。我的意思是有没有更简单的方法或 Pandas 技巧

我的计算是这样的:

goal_diff = {}
teams = df["Team"].unique()
for t in teams:
goal_diff[t] = df[df["Team"] == t]["Goal Scored"].sum() \
- df[df["Opponent"] == t]["Goal Scored"].sum()

感谢您的回复。

最佳答案

您可以通过 GroupBy.sum 进行聚合,减去并最后转换为 dict:

goal_diff = ((df.groupby('Team')["Goal Scored"].sum() - 
df.groupby('Opponent')["Goal Scored"].sum()).to_dict())
print (goal_diff)

{'Argentina': -3, 'Australia': -3, 'Belgium': 10,
'Brazil': 5, 'Colombia': 3, 'Costa Rica': -3,
'Croatia': 5, 'Denmark': 1, 'Egypt': -4,
'England': 4, 'France': 8, 'Germany': -2,
'Iceland': -3, 'Iran': 0, 'Japan': -1,
'Korea Republic': 0, 'Mexico': -3, 'Morocco': -2,
'Nigeria': -1, 'Panama': -9, 'Peru': 0,
'Poland': -3, 'Portugal': 0, 'Russia': 4,
'Saudi Arabia': -5, 'Senegal': 0, 'Serbia': -2,
'Spain': 1, 'Sweden': 2, 'Switzerland': 0,
'Tunisia': -3, 'Uruguay': 4}

关于python - Pandas 2018 世界杯数据集净胜球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com