I have a dataframe such as this one:
Date Category1 Cat2 Cat3 Cat4 Value
0 2021-02-02 4310 0 1 0 1082.00
1 2021-02-03 5121 2 0 0 -210.82
2 2021-02-03 4310 0 0 0 238.41
3 2021-02-12 5121 2 2 0 -1489.11
4 2021-02-25 6412 1 0 0 -30.97
5 2021-03-03 5121 1 1 0 -189.91
6 2021-03-09 6412 0 0 0 238.41
7 2021-03-13 5121 0 0 0 -743.08
Date column has been converted into datetime format, Value is a float, other columns are strings.
I am trying to group the dataframe by month and by each level of category, such as:
Level 1 = filter over category 1 and sum values for each category for each month:
Date Category1 Value 0 2021-02 4310 1320.41 1 2021-02 5121 -1699.93 2 2021-02 6412 -30.97 3 2021-03 5121 -1489.11 4 2021-03 6412 -932.99
Level 2 = filter over category 2 alone (one output dataframe) and over the concatenation of category 1 + 2 (another output dataframe):
Date Cat2 Value 0 2021-02 0 1320.41 1 2021-02 1 -1699.93 2 2021-02 2 -30.97 3 2021-03 0 -504.67 4 2021-03 1 -189.91
Second output :
Date Cat1+2 Value 0 2021-02 43100 1320.41 1 2021-02 51212 -1699.93 2 2021-02 64121 -30.97 3 2021-03 51210 -743.08 4 2021-03 51211 -189.91 5 2021-03 64120 238.41
Level 3 : filter over category 3 alone and over category 1+2+3
etc.
I am able to do one grouping at a time (by date or by category) but I can't combine them. Grouping by date:
df.groupby(df["Date"].dt.year)
Grouping by category:
df.groupby('Category1')['Value'].sum()