简体   繁体   English

如何在 Pandas 中分组、计数然后绘制条形图?

[英]How to do I groupby, count and then plot a bar chart in Pandas?

I have a Pandas dataframe that looks like the following.我有一个如下所示的Pandas数据框。

year  month  class
----  -----  -----
2015  1      1
2015  1      1
2015  1      2
2015  1      2
...

I want to be able to create 2 bar chart series of of this data on one plot.我希望能够在一个图上创建此数据的 2 个条形图系列。 If I can do a groupby , count and end up with a data frame then I am thinking I can just do a simple dataframe.plot.barh .如果我可以做一个groupbycount并最终得到一个data frame那么我想我可以做一个简单的dataframe.plot.barh

What I have tried is the following code.我尝试过的是以下代码。

x = df.groupby(['year', 'month', 'class'])['class'].count()

What x ends up being is a Series . x最终是一个Series So then I do the following to get a DataFrame .那么我执行以下操作以获得DataFrame

df = pd.DataFrame(x)

Which gets me pretty close.这让我非常接近。 The data ends up looking like the following.数据最终如下所示。

clazz
year month clazz        
2015 1     1            2
     2     1           15
     2     2           45

But when I do a bar plot df.plot.bar() , I only see one series.但是当我做一个条形图df.plot.bar() ,我只看到一个系列。 The output desired is simply in one series, from 2015-01 to 2019-12, how many times did class 1 occur per month?所需的输出只是一个系列,从 2015-01 到 2019-12, class 1 class每月发生多少次? And then another series, from 2015-01 to 2019-12, how many times did class 2 occur per month?然后是另一个系列,从 2015-01 到 2019-12, class 2 class每月发生多少次?

Any ideas on how to manipulate the data to be in this way?关于如何以这种方式操纵数据的任何想法?

A groupby - unstack should do the trick: groupby - unstack应该可以解决问题:

Data数据

df = pd.DataFrame([[2015, 1, 1],
                    [2015, 1, 1],
                    [2015, 1, 2],
                    [2015, 1, 2],
                    [2015, 1, 2],
                    [2015, 2, 1],
                    [2015, 2, 1],
                    [2015, 2, 1],
                    [2015, 2, 2],
                    [2015, 2, 2]], columns = ['year', 'month', 'class'])

Solution解决方案

df_gb = df.groupby(['year', 'month', 'class']).size().unstack(level=2)

Output输出

df_gb.plot(kind = 'bar')

在此处输入图片说明

We can also use DataFrame.pivot_table :我们也可以使用DataFrame.pivot_table

df.pivot_table(index=['year','month'],columns='class',aggfunc='size').plot(kind='bar')

在此处输入图片说明


or或者

df.pivot_table(index='class',columns=['year','month'],aggfunc='size').plot(kind='bar')

在此处输入图片说明

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

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