I have a dataframe:
id is_good
a1 1
a1 1
a1 0
bb 1
bb 0
bb 0
bb 0
I want to count percentage of 0 and 1 for each id. So desired result is:
id is_good_perc
a1 0.67
bb 0.25
How to do that? What should I do after groupby("id")
?
CodePudding user response:
General case:
df.groupby("id").is_good.value_counts(normalize=True)
# id is_good
# a1 1 0.666667
# 0 0.333333
# bb 0 0.750000
# 1 0.250000
# Name: is_good, dtype: float64
In your case, because you have binary variables, I'd take advantage of what @sushanth put in the comments and use df.groupby('id')['is_good'].mean()
.