I have a similiar question to this one and to this one. But the main difference is that I do not have any timestamp column, my data looks the following:
df = df.groupby(['A', 'B', 'Year', 'Month']['Sales'].sum().reset_index().sort_values(by=(['A','B','Year','Month']))
A B Year Month Sales
Der Die 2019 1 2
Der Die 2019 2 3
...
Die Das 2019 1 40
Die Das 2019 2 30
How can i get the difference for each group (pair of A and B) to the previous month (in this case previos row), that I get the following output:
A B Year Month Sales Diff
Der Die 2019 1 2 0
Der Die 2019 2 3 3/2
...
Die Das 2019 1 40 0
Die Das 2019 2 30 30/40
Note that in case of missing values, i want to have a 0 in it.
When I use:
df['diff'] = df.groupby(['A', 'B'])['Sales'].diff().fillna()
I only get the difference, but not the percentage:
A B Year Month Sales Diff
Der Die 2019 1 2 0
Der Die 2019 2 3 3-2
...
Die Das 2019 1 40 0
Die Das 2019 2 30 30-40