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

Viewed 2356

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

| date      | Revenue | Cost |
|-----------|---------|------|
| 6/1/2017  | 100     | 20   |
| 5/21/2017 | 200     | 40   |
| 5/21/2017 | 300     | 60   |
| 6/20/2017 | 400     | 80   |
| 6/1/2017  | 500     | 100  |

I need to group the above data by Month and then by Day to get output as:

| Month | Day | SUM(Revenue) | SUM(Cost) |
|-------|-----|--------------|-----------|
| May   | 21  | 500          | 100       |
| June  | 1   | 600          | 120       |
| June  | 20  | 400          | 80        |

I tried this code but it did not work:

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

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

1 Answers
Related