Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading