简体   繁体   English

来自 groupby 和 count 的堆积条形图

[英]stacked bar graph from groupby and count

I have a data frame like this after a groupby and count operation:groupbycount操作之后,我有一个这样的数据框:

timestamp status 2018-08-27 0 15 1 5 2 28 2018-08-28 0 20 1 10 2 58 ...

When I try and plot it using df.plot.bar(stacked=True) I get the following graph which is not correct.当我尝试使用df.plot.bar(stacked=True)绘制它时,我得到以下不正确的图表。 How can I actually plot my calculation as a stacked bar graph?我如何将我的计算实际绘制为堆叠条形图?

在此处输入图片说明

You need reshape Series with MultiIndex by unstack to DataFrame :你需要重塑Series具有MultiIndexunstackDataFrame

print (df.unstack(fill_value=0))
status       0   1   2
timestamp             
2018-08-27  15   5  28
2018-08-28  20  10  58

df.unstack(fill_value=0).plot.bar(stacked=True)

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM