1

I want to plot this data frame but I get an error. this is my df:

            6month  final-formula   Question Text
166047.0       1       0.007421         bathing
166049.0       1       0.006441        dressing
166214.0       1       0.001960         feeding
166216.0       2       0.011621         bathing
166218.0       2       0.003500        dressing
166220.0       2       0.019672         feeding
166224.0       3       0.012882         bathing
166226.0       3       0.013162        dressing
166229.0       3       0.008821         feeding
160243.0       4       0.023424         bathing
156876.0       4       0.000000        dressing
172110.0       4       0.032024         feeding

how can I plot a stacked bar based on the Question text? I tried some codes but raises error.

dffinal.groupby(['6month','Question Text']).unstack('Question Text').plot(kind='bar',stacked=True,x='6month', y='final-formula')
import matplotlib.pyplot as plt
plt.show()

Actually I want the 6month column be in the x-axis, final-formula in the y-axis and Question text being stacked.

so as here I have three kind of Question text, three stacked bar should be there. and as I have 4 month, 4 bars totally.

Something like this but I applied this and did not work. Am I missing something?

this picture is without stacking them. its like all question text has been summed up. I want for each Question Text there be stacked.

enter image description here

sariii
  • 2,020
  • 6
  • 29
  • 57

1 Answers1

2

You missed aggregation step after groupby, namely, sum()

df = dffinal.groupby(['6month','Question Text']).sum().unstack('Question Text')
df.columns = df.columns.droplevel()
df.plot(kind='bar', stacked=True)

I dropped multiindex level from columns just for legend consistency.

taras
  • 6,566
  • 10
  • 39
  • 50
  • well, you are right, previously I had the aggregation function but there was other mistakes. cheers to the response and your time. it worked. one more question, is there any way to launch this stack bar without using group by and aggregation? – sariii Jun 17 '18 at 21:46
  • Also, I have another numeric column in my dataframe that I deleted from df to run your code successfully(bcaz it added that numeric to final-formula so the result showing in y-axis was incorrect) . how can I keep that column and at the same time this code runs correctly? – sariii Jun 17 '18 at 21:56
  • You can drop that column while building `df` with `drop(column_name, 1)` before `groupby`. – taras Jun 17 '18 at 21:59
  • Is there anything preventing you from using a `groupby`? It seems to me this way is quite natural and intuitive. – taras Jun 17 '18 at 22:01
  • Thanks it seems a good idea :) . well, for the second question, I have changed the df a lot to be in this step, and as this df is the final one and almost everything is ok with it, I was thinking maybe I can launch it without applying group by on it. – sariii Jun 17 '18 at 22:14
  • 1
    It fact, groupby is a key function merging your data, I cannot think of any other way of doing it as for now. – taras Jun 17 '18 at 22:21