I have a question regarding plotting grouped DataFrame data.
The data looks like:
data =index taste food0 good cheese
1 bad tomato
2 worse tomato
3 worse cheese
4 good meat
5 good meat
6 bad cheese
7 worse tomato
8 worse tomato
9 good cheese
10 worse meat
11 good meat
What I want to do is to have a bar plot having each taste category as x-axis (good, bad, worse) and the percentage distribution of each food-type within each taste category as bars.
So, looking at e.g. taste category worse
we have: 3 tomato
, 1 cheese
and 1 meat
. In total there are 3+1+1=5 food-types in the category, and hence:
3/5=60% tomato
,
1/5=20% cheese
and
1/5=20% meat
So far I have tried to use GroupBy
and agg
with something like:
df_1 = data.groupby(['taste', 'food']).agg({'taste' : 'count'})
df_2 = df_1.groupby(level=0).apply(lambda x: 100 * x / float(x.sum()))
which seems to yield my wanted result:
taste
taste food
bad cheese 50.0tomato 50.0
good cheese 40.0meat 60.0
worse cheese 20.0meat 20.0tomato 60.0
But now I am stuck on how to actually plot this!
In Excel, it would look something like: