熊猫聚合图

时间:2018-09-26 12:33:26

标签: python pandas matplotlib

z=t.groupby("CUSAGE").aggregate({'ZDIVND':['nunique','count']})

我想根据上面的代码绘制一个条形图,以显示“ CUSAGE”与ZDIVND's计数之间的关系。以上任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

我认为可以使用另一种解决方案来避免列中的MultiIndex-在groupby之后指定带有聚合函数列表的列:

t = pd.DataFrame({
        'CUSAGE':list('aaaccc'),
         'ZDIVND':[4,5,4,5,5,5]
})

print (t)
  CUSAGE  ZDIVND
0      a       4
1      a       5
2      a       4
3      c       5
4      c       5
5      c       5

z=t.groupby("CUSAGE")['ZDIVND'].agg(['nunique','count'])
print (z)
        nunique  count
CUSAGE                
a             2      3
c             1      3

然后:

#if want plot both columns together
z.plot.bar()

#if want plot only count column
z['count'].plot.bar()

或使用GroupBy.count

t.groupby("CUSAGE")['ZDIVND'].count().plot.bar()