I have the following dataframe:
df.index = df['Date']
df.groupby([df.index.month, df['Category'])['Amount'].sum()
Date Category Amount
1 A -125.35
B -40.00
...
12 A 505.15
B -209.00
I would like to report the sum of the Amount for every Category B like:
Date Category Amount
1 B -40.00
...
12 B -209.00
I tried the df.get_group method but this method needs tuple that contains the Date and Category key. Is there a way to filter out only the Categories with B?
df.query('Category == "B"').groupby([df.index.month, df['Category'])['Amount'].sum()?ValueError: Grouper and axis must be same lengthdf.query('Category == "B"').groupby([df['Date'].dt.month, df['Category'])['Amount'].sum(), now you can also skip your line of settings the new indexdf['Date'].dt.monthpoints to original index. Trycat_b = df.query('Category == "B"')Then after thatcat_b.groupby([cat_b.dt.month, 'Category'])['Amount'].sum()