0

I have a question regarding plotting grouped DataFrame data.

The data looks like:

data =

index    taste    food

0        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.0
      tomato   50.0
good  cheese   40.0
      meat     60.0
worse cheese   20.0
      meat     20.0
      tomato   60.0

But now I am stuck on how to actually plot this!

In Excel, it would look something like:

enter image description here

gussilago
  • 922
  • 3
  • 12
  • 27
  • Is this example that can help you? http://seaborn.pydata.org/examples/factorplot_bars.html – Zealseeker Mar 23 '17 at 09:16
  • Thanks @Zealseeker. I'll have a look. Classic that one founds the answer to your own question while processing it and typing it here :) – gussilago Mar 23 '17 at 09:21

1 Answers1

0

I seem to have found an example:

making a stacked barchart in pandas

Sufficient doing:

df_3 = df_2.unstack()
df_3.plot(kind='bar',stacked=True, rot=1)
Community
  • 1
  • 1
gussilago
  • 922
  • 3
  • 12
  • 27