pandas 聚合图
问题描述:
z=t.groupby("CUSAGE").aggregate({'ZDIVND':['nunique','count']})
我想根据上面的代码绘制一个条形图,显示"CUSAGE
"与ZDIVND's
计数的关系.在上述任何帮助将不胜感激.
I would like to plot a bar graph for "CUSAGE
" vs ZDIVND's
count from the above code. Any help on the above would be appreciated.
谢谢
答
我认为可以使用另一种解决方案来避免列中的MultiIndex
聚集-在groupby
指定带有聚集函数列表的列之后:
I think use another solution for aggregation for avoid MultiIndex
in columns - after groupby
specify column with list of aggregation functions:
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()