简体   繁体   English

如何计算组内值的平均百分比?

[英]How to calculate average percentages of values within group?

I have a dataframe:我有一个数据框:

date             type
2021-08-12       fail
2021-08-12       fail
2021-08-12       win
2021-08-12       great_win
2021-08-13       fail
2021-08-13       win
2021-08-13       win
2021-08-13       win

I want to calculate percentage of each 'type' within date group and then average values among all dates.我想计算日期组中每种“类型”的百分比,然后计算所有日期之间的平均值。 So desired results must be:所以想要的结果必须是:

date             type          type_perc
2021-08-12       fail           0.5
2021-08-12       win            0.25
2021-08-12       great_win      0.25
2021-08-13       fail           0.25
2021-08-13       win            0.75
2021-08-13       great_win      0.0

and then average among all dates.然后平均所有日期。 this is the desired final result:这是所需的最终结果:

type          type_perc
fail           0.375
win            0.5
great_win      0.175

How to do that?怎么做?

You can try this:你可以试试这个:

tmp = df.groupby(['date', 'type']).size()/df.groupby('date')['type'].size()
print(tmp)

date        type
2021-08-12  fail         0.50
            great_win    0.25
            win          0.25
2021-08-13  fail         0.25
            win          0.75
dtype: float64

result = tmp.groupby(level=1).sum()/tmp.sum()
print(result)

type
fail         0.375
great_win    0.125
win          0.500
dtype: float64

or this:或这个:

result = tmp.groupby(level=1).mean()
print(result)
type
fail         0.375
great_win    0.250
win          0.500
dtype: float64

It's not quite clear by your question你的问题不是很清楚

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

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