I have a function using the variable b (t-1):
def test_b(a,b_1):
return a + b_1
Suppose the following frame:
df = pd.DataFrame({'a':[1,2,3],'b':np.nan})
I assign the initial value b_1:
df['b'].ix[0]=0
and then (using my Matlab experience), I use a loop:
for i in range(1,len(df)):
df['b'].ix[i] = test_b(df['a'].ix[i],df['b'].ix[i-1])
conclusion:
a|b
0|1|0
1|2|2
2|3|5
Is this a more elegant way to do the same?
source
share