Braden
Braden

Reputation: 759

Calculating new DataFrame column based on original column and preceding cell in new column

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

Answers (1)

jezrael
jezrael

Reputation: 863146

You need divide by div and then cumsum:

df['new'] = df['interval'].div(1000).cumsum()
   interval    new
0       670  0.670
1       664  1.334
2       680  2.014
3       672  2.686
4       673  3.359

Upvotes: 1

Related Questions