this code gives the result i want:
- it takes the value n-1 and calculates n from it
take the previous value in column 'y' lets call it y-1 and calculate a value which becomes the new y, than in the next row take that new y as y-1 and calculate another new y aso
size = 10
x= range(1,size+1)
df = pd.DataFrame(data={'x': x,'y': size })
for n in range(1,len(x)):
df['y'].iloc[n] = df['y'].iloc[n-1]*2
out:
x y
0 1 10
1 2 20
2 3 40
... ... ...
9 10 5120
I want to put it into a lambda but somehow fail to get it right:
b=2
df['y'].loc[1::] = df['y'].shift(-1).apply(lambda x: x*b)
out:
x y
0 1 10.0
1 2 20.0
2 3 20.0
... ... ...
the lambda function takes the pre-populated value (10) in each row instead of shifting 1 step back and taking the previous value as base for the multiplication
i looked at some threads, but its above my comprahension, if i am dealing here with recursion and this is not possible with lambdas?
recursive lambda-expressions possible?
Can a lambda function call itself recursively in Python?
Edit: I want that subsequent entries in 'y' are calculated with previous 'y' entries, starting from idx 1
DataFrame at start:
idx | y |
0 10
DataFrame after 1st calc:
y1 = y0 *2
# *2 is a placeholder could be mx+b, or something else
idx | y |
0 10
1 20