Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Group By Month from a Date field using Python/Pandas

I have a Data-frame df which is as follows:

| date      | Revenue | |-----------|---------| | 6/2/2017  | 100     | | 5/23/2017 | 200     | | 5/20/2017 | 300     | | 6/22/2017 | 400     | | 6/21/2017 | 500     | 

I need to group the above data by month to get output as:

| date | SUM(Revenue) | |------|--------------| | May  | 500          | | June | 1000         | 

I tried this code but it did not work:

df.groupby(month('date')).agg({'Revenue': 'sum'}) 

I want to only use Pandas or Numpy and no additional libraries

like image 445
Symphony Avatar asked Jul 04 '17 14:07

Symphony


People also ask

How do I sort month columns in pandas?

df = df. sort_values(by='date',ascending=True,inplace=True) works to the initial df but after I did a groupby , it didn't maintain the order coming out from the sorted df . To conclude, I needed from the initial data frame these two columns. Sorted the datetime column and through a groupby using the month (dt.


2 Answers

try this:

In [6]: df['date'] = pd.to_datetime(df['date'])  In [7]: df Out[7]:          date  Revenue 0 2017-06-02      100 1 2017-05-23      200 2 2017-05-20      300 3 2017-06-22      400 4 2017-06-21      500    In [59]: df.groupby(df['date'].dt.strftime('%B'))['Revenue'].sum().sort_values() Out[59]:  date May      500 June    1000 
like image 51
shivsn Avatar answered Sep 21 '22 01:09

shivsn


Try a groupby using a pandas Grouper:

df = pd.DataFrame({'date':['6/2/2017','5/23/2017','5/20/2017','6/22/2017','6/21/2017'],'Revenue':[100,200,300,400,500]}) df.date = pd.to_datetime(df.date) dg = df.groupby(pd.Grouper(key='date', freq='1M')).sum() # groupby each 1 month dg.index = dg.index.strftime('%B')       Revenue  May    500 June    1000 
like image 28
qbzenker Avatar answered Sep 22 '22 01:09

qbzenker