Search code examples
pythonpandasdataframegroup-bypercentage

Get frequency of item occurrences in a column as percentage


I want to get a percentage of a particular value in a df column. Say I have a df with (col1, col2 , col3, gender) gender column has values of M, F, or Other. I want to get the percentage of M, F, Other values in the df.

I have tried this, which gives me the number M, F, Other instances, but I want these as a percentage of the total number of values in the df.

df.groupby('gender').size()

Can someone help?


Solution

  • Use value_counts with normalize=True:

    df['gender'].value_counts(normalize=True) * 100
    

    The result is a fraction in range (0, 1]. We multiply by 100 here in order to get the %.