Sanket Deshmukh
Sanket Deshmukh

Reputation: 186

how to plot multi indexed dataframe in python using python?

I have multi-indexed dataframe with single column. I want to plot stacked bar graph based on that dataframe. The data is as follows:

df= pd.DataFrame(index=pd.MultiIndex([[1,2,3],['open','closed']],[[0,0,1,1,2,2],[0,1,0,1,0,1]]))

df['id']=[23,6,12,4,31,16]
df
        id
state
1 closed 23
  open    6
2 closed 12
  open    4
3 closed 31
  open   16

Upvotes: 4

Views: 117

Answers (1)

maxymoo
maxymoo

Reputation: 36545

You need to unstack your dataframe:

%matplotlib inline
df.unstack().plot(kind='bar', stacked=True)

enter image description here

Upvotes: 6

Related Questions