4

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
1
  • Here State is index and id is column. State consist of 'closed' and 'open'. First level indices are 1,2,3...and second level indices are 'closed', 'open' Commented Sep 7, 2015 at 6:50

1 Answer 1

6

You need to unstack your dataframe:

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.