计算分组值的百分比

时间:2021-03-28 10:48:29

标签: python-3.x pandas

我有一个 Pandas 数据框,看起来像“

enter image description here

我计算了每年的 Win、Lost 和 Draw 的数量,现在看起来像:

enter image description here

我的目标是按年计算每个分数组的百分比。变得像: enter image description here

但我卡在这里了。 我查看了 this thread,但无法将其应用于我的 df。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

这是我为此任务编写的一个非常简单的方法:

只需执行以下操作:

  1. 创建每年总分的数据框:
  • total_score = df.groupby('year')['score'].sum().reset_index(name = 'total_score_each_year')
  1. 将原始数据帧和新数据帧合并为一个数据帧:
  • df = df.merge(total_score, on = 'year')
  1. 计算百分比:
  • 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
)

详细说明:

  1. df.groupby('year').score.sum() 使用每年 score 的总和创建一个 DataFrame。
  2. df.merge 创建一个与原始 df 相同的 Dataframe,但将列 score 重命名为 score_x 和一个附加列 score_y,代表每一行当年所有分数的总和; how='left' 仅保留左侧 DataFrame 中的行,即 df
  3. .apply 使用 score_xscore_y 计算每个对应百分比(注意 axis=1 选项,逐行应用 lambda)。