简体   繁体   English

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

[英]Pandas Groupby - Calculate percentage of values per group total value

I have this Pandas group by statement: 按声明,我有这个熊猫小组:

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

which returns something like this : 返回如下内容:

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

For each team, I want to show the percentage per outcome of the team's total (not the total entries in the dataframe). 对于每个团队,我想显示每个结果占团队总数(而不是数据框中的总数)的百分比。 So something like this: 所以像这样:

outcome | teams 
--------|----------------|-----
  win   | Man utd        | 0.80
        | Chelsea        | 0.5
        | Arsenal        | 0.0625
--------|----------------|------
  loss  | Man utd        | 0.20
        | Chelsea        | 0.5
        | Arsenal        | 0.9375

Please how do I get this outcome? 请问我如何得到这个结果?

Reproducing the dataset like you have: 像您一样复制数据集:

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()

My grouped variable now looks like yours. 我的grouped变量现在看起来像您的变量。

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


Solution: 解:

grouped in your case is result of df['teams'].groupby(train_sub['outcome']).value_counts() . grouped您的情况进行groupeddf['teams'].groupby(train_sub['outcome']).value_counts() So, just do: 因此,只需:

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

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM