Question: how can I replace specific row value within a pandas method chain.
Here is my code:
days = np.arange(0,11)
rets = np.array([ 0.00, 0.02, 0.03, 0.04, -0.01, -0.02, 0.01, 0.02, -0.03, -0.05,0.10 ])
start = 100
df = pd.DataFrame({"time": days, "return":rets})
new_df = (df
.assign(**{f"lag_{i}":df["return"].add(1).iloc[1:].shift(-i).cumprod() for i in np.arange(6)})
)
new_df.iloc[0] = new_df.iloc[0].replace(np.nan,1) # add to method chain above
How can I do the operation in the last line within the method chain. With method chain I mean
new_df = (df
.assign(...)
.replace(...)
)