I want to reverse of this - Calculating YTD totals in Pandas
ID month year Fee
68119 February 2015 25
68119 March 2015 25
68119 April 2015 25
68119 May 2015 25
00200 February 2015 50
00200 March 2015 375
00200 April 2015 375
00200 May 2015 375
00200 June 2015 375
00200 July 2015 375
00200 August 2015 375
The Fee
column is the YTD numbers. I want the MTD numbers. Expected output -
ID month year Fee
68119 February 2015 25
68119 March 2015 0
68119 April 2015 0
68119 May 2015 0
00200 February 2015 50
00200 March 2015 325
00200 April 2015 0
00200 May 2015 0
00200 June 2015 0
00200 July 2015 0
00200 August 2015 0
The logic for the YTD -
df.groupby('ID')['Fee'].cumsum()
Now for MTD, I figure if there isn't a pandas
way of doing it, then I have to use a for loop on the ID
, sort
it by [year, month]
and then do the subtraction. I have tried, but I know there is a more native pandas
way of doing it. Thanks in advance.