0

I have a dataframe containing the percentage of people in my dataset stratified per Gender and Age.

df_test = pd.DataFrame(data=[['Male','16-24',10],
                             ['Male','25-34',5],
                             ['Male','35-44',2],
                             ['Female','16-24',3],
                             ['Female','25-34',60],
                             ['Female','35-444',20],
                                ],
                        columns=['Gender','Age','Percentage'])

First I create a plot showig the percentages of Male and Female in the dataset

df_test.groupby('Gender').sum().plot(kind='bar',rot=45)

enter image description here

Now I would like to add within each bar the percentage of people in the age ranges in a stacked kind of way... Could you help ?

gabboshow
  • 5,359
  • 12
  • 48
  • 98

1 Answers1

0

You can use the cumsum() method for cumulative summation as

df_test.groupby('Age').sum().cumsum().plot(kind="bar",rot=45)
Mostafa Ayaz
  • 480
  • 1
  • 7
  • 16