glhclhy glhclhy - 14 days ago 5
Python Question

Pandas accumulation without for loop

Hi I have a pandas frame work like:

1. 1
2. 2
3. 3
4. 4


And the output is something like

1. 1
2. 3
3. 6
4. 10


where each value is the current value plus the last one (3 = 1 + 2, 6 = 3 + 3, 10 = 6 + 4 etc).

Can I do this without a for loop?

Answer

You need Series.cumsum:

print (df)
     col
1.0    1
2.0    2
3.0    3
4.0    4

df['col1'] = df.col.cumsum()
print (df)
     col  col1
1.0    1     1
2.0    2     3
3.0    3     6
4.0    4    10

If need overwrite column col:

df.col = df.col.cumsum()
print (df)
     col
1.0    1
2.0    3
3.0    6
4.0   10