0

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")?

french_fries
  • 1,149
  • 6
  • 22
  • 2
    Does this answer your question? [Pandas percentage of total with groupby](https://stackoverflow.com/questions/23377108/pandas-percentage-of-total-with-groupby) – Chris Oct 13 '21 at 13:31
  • 1
    ``df.groupby('id', as_index=False)['is_good'].mean()`` – sushanth Oct 13 '21 at 13:34

1 Answers1

0

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

arnaud
  • 3,293
  • 1
  • 10
  • 27