Home > Enterprise >  plotting percentage of occurrence in a group in data frame python
plotting percentage of occurrence in a group in data frame python

Time:08-22

I have a data frame with two columns, age group and gender. I want to plot the percentage of females and males in every age group.

this is what i did

df.groupby('AGE_B_M0')['Gender_Type_Cd_M0'].value_counts(normalize=True)

how do i plot this as pie chart ?

I got the percentages correctly but i want to plot the percentage of genders in all age groups.

for example : females 30% and males 70% in age group 18-25 etc..

CodePudding user response:

df.groupby('Gender_Type_Cd_M0').count() / len(df) should return the percentage of observations for each gender.

  • Related