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

后端 未结 5 1987
孤独总比滥情好
孤独总比滥情好 2021-02-02 10:57

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

| date      | Revenue |
|-----------|---------|
| 6/2/2017  | 100     |
| 5/23/2017 | 200     |
| 5/20/2017 | 300             


        
5条回答
  •  孤城傲影
    2021-02-02 11:22

    This will work better.

    Try this:

    #explicitly convert to date
    df['Date'] = pd.to_datetime(df['Date'])
    # set your date column as index 
    df.set_index('Date',inplace=True) 
    
    # For monthly use 'M', If needed for other freq you can change.
    df[revenue].resample('M').sum()
    

    This code gives same result as @shivsn answer on first post.

    But thing is we can do lot more operations in this mentioned code. Recommended to use this:

    >>> df['Date'] = pd.to_datetime(df['Date'])
    >>> df.set_index('Date',inplace=True)
    >>> df['withdrawal'].resample('M').sum().sort_values()
    Date
    2019-10-31     28710.00
    2019-04-30     31437.00
    2019-07-31     39728.00
    2019-11-30     40121.00
    2019-05-31     46495.00
    2020-02-29     57751.10
    2019-12-31     72469.13
    2020-01-31     76115.78
    2019-06-30     76947.00
    2019-09-30     79847.04
    2020-03-31     97920.18
    2019-08-31    205279.45
    Name: withdrawal, dtype: float64
    

    where @shivsn code's does same.

    >>> df.groupby(df['Date'].dt.strftime('%B'))['withdrawal'].sum().sort_values()
    Date
    October       28710.00
    April         31437.00
    July          39728.00
    November      40121.00
    May           46495.00
    February      57751.10
    December      72469.13
    January       76115.78
    June          76947.00
    September     79847.04
    March         97920.18
    August       205279.45
    Name: withdrawal, dtype: float64
    

提交回复
热议问题