I have a table that looks like this:
date revenue
2021-01-01 20
2021-01-02 30
...
2021-01-31 50
2021-02-01 35
2021-02-02 67
I want to calculate for each row maximum revenue for the previous month.
I can calculate maximum revenue for the current month:
df['max']=df.groupby(df['date'].dt.month)[revenue'].transform(max)
And it will look like this:
date revenue max
2021-01-01 20 50
2021-01-02 30 50
...
2021-01-31 50 50
2021-02-01 35 67
2021-02-02 67 67
But I want it to be:
date revenue max
2021-01-01 20 nan
2021-01-02 30 nan
...
2021-01-31 50 nan
2021-02-01 35 50
2021-02-02 67 50
I tried to put .shift() in the end but it will only lag/lead max by rows, yet I need it by group.
Please help