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 cumsum with keys

I have two DataFrames (first, second):

index_first value_1 value_2
0 100 1
1 200 2
2 300 3
index_second value_1 value_2
0 50 10
1 100 20
2 150 30

Next I concat the two DataFrames with keys:

z = pd.concat([first, second],keys=['x','y'])

My goal is to calculate the cumulative sum of value_1 and value_2 in z considering the keys.
So the final DataFrame should look like this:

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

index_z value_1 value_2
x,0 100 1
x,1 300 3
x,2 600 6
y,0 50 10
y,1 150 30
y,2 300 60

>Solution :

Use GroupBy.cumsum by first level created by keys from concat:

df = z.groupby(level=0).cumsum()
print (df)
               value_1  value_2
  index_first                  
x 0                100        1
  1                300        3
  2                600        6
y 0                 50       10
  1                150       30
  2                300       60
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