简体   繁体   中英

Calculate percentage of specific grouped values

I have a dataframe:

id       is_good
a1        1
a1        1
a1        0
bb        1
bb        0
bb        0
bb        0
с1        0
с1        0
d2        1
d2        1

As you see there are cases when id value has only type of is_good. I want to count percentage of 0 and 1 for each id. And if id value has only type of is_good, the type that he hasn't must be 0 percent. The same with d2. So desired result is:

id       is_good_perc
a1        0.67
bb        0.25
с1        0
d2        1      

How to do that? The tricky part for me is case with id c1 and d2.

I tried this:

.groupby("id").is_good.value_count(normalize=True)

but it doesn't create 0 for c1 and d2

You group df.is_good by df.id then divide the sum by the count:

>>> grouped = df['is_good'].groupby(df.id)
>>> df1 = pd.DataFrame(grouped.sum() / grouped.count()).round(2).rename(columns={'is_good': 'is_good_perc'})
>>> df1 
    is_good_perc
id              
a1          0.67
bb          0.25
d2          1.00
с1          0.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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