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

Pandas multicolumn cumsum() on a subset of rows

I can get this to work if I do the cumsum one column at a time. However doing it together outputs NaNs.

df = pd.DataFrame({"category": [1,2,2], "value1": [10,11,12], "value2": [20,21,22]})
m = df["category"] == 2
df.loc[m, ["cum_value1", "cum_value2"]] = df.loc[m, ["value1", "value2"]].cumsum()

>Solution :

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

You can just assign new column

df[["cum_value1", "cum_value2"]] = df.loc[m, ["value1", "value2"]].cumsum()
df
Out[520]: 
   category  value1  value2  cum_value1  cum_value2
0         1      10      20         NaN         NaN
1         2      11      21        11.0        21.0
2         2      12      22        23.0        43.0
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