如何使用python在python中绘制多索引数据帧?

时间:2015-09-07 06:42:16

标签: python-2.7 pandas plot ipython dataframe

我有单列的多索引数据框。我想基于该数据帧绘制堆积条形图。数据如下:

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 个答案:

答案 0 :(得分:6)

您需要unstack您的数据框:

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

enter image description here