Pandas Groupby-计算每组总价值的百分比

时间:2019-08-18 08:01:39

标签: python pandas pandas-groupby

我有这个Pandas分组声明:

.toISOString()

返回如下内容:

df['teams'].groupby(train_sub['outcome']).value_counts()

对于每个团队,我想显示每个结果占团队总数(而不是数据框中的总数)的百分比。像这样:

outcome | teams 
--------|----------------|-----
  win   | Man utd        | 120
        | Chelsea        | 75
        | Arsenal        | 10
--------|----------------|------
  loss  | Man utd        | 30
        | Chelsea        | 75
        | Arsenal        | 150

请问我如何得到这个结果?

1 个答案:

答案 0 :(得分:1)

像复制数据集一样:

df = pd.DataFrame()
df['outcome'] = ['win', 'win', 'win', 'loss', 'loss', 'loss']
df['teams'] = ['manu', 'chelsea', 'arsenal', 'manu', 'chelsea', 'arsenal']
df['points'] = [120, 75, 10, 30, 75, 150]
grouped = df.groupby(['outcome', 'teams'])['points'].sum()

我的grouped变量现在看起来像您的变量。

                 points
outcome teams          
loss    arsenal     150
        chelsea      75
        manu         30
win     arsenal      10
        chelsea      75
        manu        120


解决方案:

groupeddf['teams'].groupby(train_sub['outcome']).value_counts()的结果。因此,只需:

grouped / grouped.groupby(level = 1).sum()

输出:

outcome teams    points     
loss    arsenal  0.9375
        chelsea  0.5000
        manu     0.2000
win     arsenal  0.0625
        chelsea  0.5000
        manu     0.8000