Skip to content
Advertisement

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:

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement