简体   繁体   中英

How to group and plot values a bar chart matplotlib

I am trying to group all values into months and plot these as a bar chart . Below is what I have tried thus far:

import pandas as pd

d1 = ({
    'Date' : ['1/7/18','1/7/18','1/8/18','1/8/18','1/9/18'],     
    'Value' : ['Foo','Bar','Foo','Bar','Foo'],           
    })

df1 = pd.DataFrame(data = d1)

df1['Date'] = pd.to_datetime(df1['Date'])
df1.set_index('Date', inplace = True)
df1.resample('1M').count()['Value'].plot(kind = 'bar')

But this only produces one bar with a count of 5 . I'm hoping the intended output would be 3 separate bars . A count of 2 for July , 2 for August , and 1 for September .

Problem is with converting to datetimes, need set format or dayfirst=True , because DD/MM/YY :

df1['Date'] = pd.to_datetime(df1['Date'], format='%d/%m/%y')

Or:

df1['Date'] = pd.to_datetime(df1['Date'], dayfirst=True)

And if need plot by month names use:

df1['Date'] = pd.to_datetime(df1['Date'], format='%d/%m/%y').dt.month_name()
#alternative
#df1['Date'] = pd.to_datetime(df1['Date'], format='%d/%m/%y').dt.strftime('%B')
df1.groupby('Date')['Value'].count().plot(kind = 'bar')

G

If need correct ordering of months:

months = ['January','February','March','April','May','June','July','August',
          'September','October','November','December']

df1['Date'] = pd.Categorical(df1['Date'], categories=months, ordered=True)
df1.groupby('Date')['Value'].count().plot(kind = 'bar')

G1

If want filter out 0 values:

df1.groupby('Date')['Value'].count().pipe(lambda x: x[x != 0]).plot(kind = 'bar')

G2

Thanks @asongtoruin for another idea:

df1['Date'] = pd.to_datetime(df1['Date'], format='%d/%m/%y') 
#if necessary sorting datetimes
#df1 = df1.sort_values('Date')

df1['month_name'] = df1['Date'].dt.month_name()

df1.groupby('Date').agg({'Value': 'count', 'month_name': 'first'})
                   .plot(x='month_name', y='Value', kind='bar')

Your code works just fine, but you've mixed up the day/month format

All you need to do is change

'Date' : ['1/7/18','1/7/18','1/8/18','1/8/18','1/9/18'], 

To

'Date' : ['7/1/18','7/1/18','8/1/18','8/1/18','9/1/18'],

另一种解决方案是使用数据透视表按日期分组。

pd.pivot_table(df1, values='Value', index='Date', aggfunc='count').plot(kind='bar')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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