我有datafrme,我在两列上执行groupby:
ax = df_all.groupby(['Component', 'SubComponent' ])['SubComponent'].count()
这给了我这个:
--------------------------------------------------
| Component | SubComponent| |
--------------------------------------------------
|A |First | 1|
--------------------------------------------------
| |Second | 10|
--------------------------------------------------
| |Third | 6|
--------------------------------------------------
|B |First | 8|
--------------------------------------------------
| |Second | 5|
--------------------------------------------------
| |Third | 17|
--------------------------------------------------
现在我想用它做一个直方图。 每个条形都是Component,每个组件将在SubComponents上划分,显示每个SubComponent的出现次数不同。
使用matplotlib.pyploy有没有简单的方法来完成它?
谢谢, Submi
答案 0 :(得分:2)
您可以unstack
使用DataFrame.plot.bar
:
print (ax.unstack())
SubComponent First Second Third
Component
A 1 4 15
B 6 2 1
ax.unstack().plot.bar(stacked=True)
print (ax.unstack(0))
Component A B
SubComponent
First 1 6
Second 4 2
Third 15 1
ax.unstack(0).plot.bar(stacked=True)
通知: