Pandas get frequency of item occurrences in a column as percentage [duplicate]

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 %.


If you do not need to look M and F values other than gender column then, may be you can try using value_counts() and count() as following:

df = pd.DataFrame({'gender':['M','M','F', 'F', 'F']})
# Percentage calculation
(df['gender'].value_counts()/df['gender'].count())*100

Result:

F    60.0
M    40.0
Name: gender, dtype: float64

Or, using groupby:

(df.groupby('gender').size()/df['gender'].count())*100

Let's say there are 200 values out of which 120 are categorized as M and 80 as F

1)

df['gender'].value_counts()

 output:

 M=120
 F=80

2)

df['gender'].value_counts(Normalize=True)

  output:

  M=0.60
  F=0.40

3)

df['gender'].value_counts(Normalize=True)*100 #will convert output to percentages

  output:

  M=60
  F=40