Dataframe:
df = pd.DataFrame({"X":[1,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]})
print(df)
X
0 1.0
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
I want to populate np.nan by taking squares of values from previous row and adding one to it.
Desired output:
X
0 1.0
1 2.0
2 5.0
3 26.0
4 677.0
5 458330.0
This can be done by a for loop with:
for i in range(1,len(df)):
df["X"].iloc[i] = ((df["X"].iloc[i-1]) ** 2) + 1
But looking for a vectorised solution of the same problem