I have a dataframe where the first row is the initial condition.
df = pd.DataFrame({"Year": np.arange(4),
"Pop": [0.4] + [np.nan]* 3})
and a function f(x,r) = r*x*(1-x), where r = 2 is a constant and 0 <= x <= 1.
I want to produce the following dataframe by applying the function to column Pop row-by-row iteratively. I.e., df.Pop[i] = f(df.Pop[i-1], r=2)
df = pd.DataFrame({"Year": np.arange(4),
"Pop": [0.4, 0.48, 4992, 0.49999872]})
Question: Is it possible to do this in a vectorized way?
I can achieve the desired result by using a loop to build lists for the x and y values, but this is not vectorized.
I have also tried this, but all nan places are filled with 0.48.
df.loc[1:, "Pop"] = R * df.Pop[:-1] * (1 - df.Pop[:-1])
np.arange(51)gives error because the lengths of lists do not match. Please note that the primary purpose of an example is for the potential helpers to reproduce your problem, so focus on the relevant part only and simplify others as much as possible. I am editing your post currently. Please wait for it to take effect.