0

I need to sum a column but need to see how much each row has incremented by so that I can plot the sequence on a chart. I am using a loop but on a large dataset with thousands of rows it is very slow. Is there a more efficient pythonesque way to do this?

hh = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))

x=0
for i, row in hh.iterrows(): 
    x = row["D"] + x
    row['z'] = x 
    hh.at[i,'z'] = x

print(hh)

So, Z2 = z1+D2

Z3 = z2+D3

z4 = z3+D4 ... and so on

   A  B  C  D     z
0  7  8  7  9   9.0
1  3  8  7  7  16.0
2  0  4  4  3  19.0
3  7  5  8  3  22.0
4  2  1  6  1  23.0
5  2  0  2  7  30.0
6  5  4  2  7  37.0
7  3  4  0  7  44.0
8  1  3  9  2  46.0
9  9  9  0  8  54.0
Grantx
  • 315
  • 1
  • 10
  • 3
    See [`pandas.Series.cumsum`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.cumsum.html) – Sayandip Dutta Feb 13 '21 at 23:15
  • Thank you I did not know about that function. I cant mark your answer as the answer because it looks like my question will be deleted - but thank you! – Grantx Feb 13 '21 at 23:23

0 Answers0