Home > Blockchain >  How to get value_counts normalize=True with pandas agg?
How to get value_counts normalize=True with pandas agg?

Time:01-05

This gives me counts of the # my_other_var that are contained in 'my_var'

df.groupby('my_var').agg({'my_other_var' : 'value_counts')

What I want is the percentage of 'my_var' that is comprised of 'my_other_var'

I tried this, but does not seem to give me what I expect

df.groupby('my_var').agg({'my_other_var' : lambda x: pd.value_counts(x, normalize=True)})

CodePudding user response:

You can use SeriesGroupBy.value_counts :

df.groupby('my_var')['my_other_var'].value_counts(normalize=True)
  • Related