How to get value_counts normalize=True with pandas agg?

Question:

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)})

Asked By: curious

||

Answers:

You can use SeriesGroupBy.value_counts :

df.groupby('my_var')['my_other_var'].value_counts(normalize=True)
Answered By: abokey
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.