0

I have a df that looks like this.

Education                 Food_expenditures   Mitigative_expenditures   Household_expenditures
0   Primary (incomplete)    0.549451          6.923077                  2.527473
1   Illiterate              5.000000          5.000000                  0.000000
2   Primary (completed)     2.868217          4.883721                  2.248062
4   Illiterate              5.593220          3.559322                  0.847458
5   Primary (incomplete)    3.333333          5.555556                  1.111111

I would like to have a horizontal bar plot that is grouped by the expenditures and then by education. Something that looks like this (only this example is just grouped by two levels for the second part).

enter image description here

I also want to invert it, so group first for Education and then for expenditures. But I'm sure I can figure that out based on the first one. I just couldn't find a good example.

1 Answers1

1

First aggregate values by Education by some function like sum or mean and transpose DataFrame:

df1 = df.groupby('Education').sum().T
print (df1)

Education                Illiterate  Primary (completed)  Primary (incomplete)
variable                                                                      
Food_expenditures         10.593220             2.868217              3.882784
Household_expenditures     0.847458             2.248062              3.638584
Mitigative_expenditures    8.559322             4.883721             12.478633

Then use DataFrame.plot.barh:

df1.plot.barh()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you for your answer! Would this also work if I want to have Education as the group? So three bars (food,household,mitigative) per education level? – Joep Hoeijmakers Feb 22 '21 at 07:37
  • @JoepHoeijmakers - So here are 9 bars grouped by 3? – jezrael Feb 22 '21 at 07:45
  • @JoepHoeijmakers - Or need [this](https://stackoverflow.com/questions/22787209/how-to-have-clusters-of-stacked-bars-with-python-pandas) ? – jezrael Feb 22 '21 at 08:30
  • 9 bars grouped by three indeed. so for every level in my column Education, the corresponding bars for food/household/mitigative expenditures – Joep Hoeijmakers Feb 22 '21 at 09:46