Search code examples
python-3.xpandaspandas-groupbyepochsumifs

Pandas groupby multiple columns basis date column by epoch week


I want to sum up three columns that I have got in my data frame basis the date column. The catch here is that the sum up should happen basis the epoch week range. Fro example: epoch week 14 is from 30th March 2020 to 5th April 2020 and week 15 is 6th April 2020 to 15th April 2020 and so on. The date_time column is already in the correct datetime format. Epoch weeks can be found here :epochweek

My data frame looks like this :

   date_time                    A        B
0  01/04/2020  00:00:00         5        10
1  02/04/2020  00:00:00         5        10
2  03/04/2020  00:00:00         1        10
3  07/04/2020  00:00:00         2        10
4  08/04/2020  00:00:00         2        10
5  10/04/2020  00:00:00         3        10

The output should look like the following:

epoch_week       sum_A       sum_B
week14           11          30
week15           7           30
Total            18          60

Solution

  • Use Series.dt.weekofyear with datetimes and aggregate sum:

    g = pd.to_datetime(df['date_time'], dayfirst=True).dt.weekofyear
    
    df = df.groupby(g.rename('epoch_week')).sum().reset_index()
    print (df)
       epoch_week   A   B
    0          14  11  30
    1          15   7  30
    

    EDIT:

    g = pd.to_datetime(df['date_time'], dayfirst=True).dt.weekofyear
    
    df = df.groupby(g.rename('epoch_week')).sum()
    df.loc['Total'] = df.sum()
    df = df.reset_index()