Reputation: 759
I am trying to calculate a new column in a DataFrame based on an existing column. The new column value needs to be the sum of the value on the same row in the original column (here 'interval') and the value of the preceding value in the new column. Diagram below shows the formula.
interval new_column
0 670 = i0
1 664 = i1 + n_c0
2 680 = i2 + n_c1
3 672 = i3 + n_c2
4 673 = i4 + n_c3
...
n ### = in + i(n - 1)
At present I have calculated it via a list and joined it to the original DataFrame.
temp = []
for i, val in enumerate(interval):
if i == 0:
temp.append(val / 1000)
else:
temp.append((val / 1000) + temp[i - 1])
I am sure there is a way to do it, I'm just not yet fluent enough in pandas.
Upvotes: 1
Views: 40