我有一个 Pandas 数据框,看起来像“
我计算了每年的 Win、Lost 和 Draw 的数量,现在看起来像:
但我卡在这里了。 我查看了 this thread,但无法将其应用于我的 df。
有什么想法吗?
答案 0 :(得分:2)
这是我为此任务编写的一个非常简单的方法:
只需执行以下操作:
total_score = df.groupby('year')['score'].sum().reset_index(name = 'total_score_each_year')
df = df.merge(total_score, on = 'year')
df['percentages'] = 100 * (df['score'] / df['total_score_each_year'])
就是这样,我希望它有帮助:)
答案 1 :(得分:0)
您可以尝试使用:df.iat[row, column]
它看起来像这样:
percentages = []
for i in range(len(df) // 3):
draws = df.iat[i, 2]
losses = df.iat[i + 1, 2]
wins = df.iat[i + 2, 2]
nbr_of_games = draws + losses + wins
percentages.append[draws * 100/nbr_of_games] #Calculate percentage of draws
percentages.append[losses * 100/nbr_of_games] #Calculate percentage of losses
percentages.append[wins * 100/nbr_of_games] #Calculate percentage of wins
df["percentage"] = percentages
这可能不是最快的方法,但我希望它有所帮助!
答案 2 :(得分:0)
类似于@panter 的回答,但仅在一行中并且不创建任何额外的 DataFrame:
df['percentage'] = df.merge(df.groupby('year').score.sum(), on='year', how='left').apply(
lambda x: x.score_x * 100 / x.score_y, axis=1
)
详细说明:
df.groupby('year').score.sum()
使用每年 score
的总和创建一个 DataFrame。df.merge
创建一个与原始 df
相同的 Dataframe,但将列 score
重命名为 score_x
和一个附加列 score_y
,代表每一行当年所有分数的总和; how='left'
仅保留左侧 DataFrame 中的行,即 df
。.apply
使用 score_x
和 score_y
计算每个对应百分比(注意 axis=1
选项,逐行应用 lambda)。