Python cumsum of rows up until n-1

I am trying to get a cumulative sum of a column in a dataframe for all rows except the row we are interested in. The dataframe is split by year.

I have been able to do this in excel and the below is what I am trying to achieve.

enter image description here

I am close by using

df1['CumSum'] = df1.groupby('Year')['Value'].cumsum()

but this will return

enter image description here

>Solution :

You can use a shift on the groups to shift the result by one:


df["CumSum"] = df.groupby("Year", group_keys=False)["Value"].apply(lambda x: x.cumsum().shift(1))

Leave a Reply