1

I run this code:

df=df_LtC
df["rolling"] = df["ln_ret"].rolling(window=2).mean() * 2

After running the function, I want to have df and df_LtC as different dataframes. I have no clue why to df_LtC is added the column 'rolling' becoming exactly the same as df.

df_LtC looks like this:

      date        variable       price    ln_ret
0   2000-01-01       low          2.03     NaN 
1   2000-01-01      close         2.39     0.16
2   2000-01-02       low          2.33    -0.02
3   2000-01-02      close         2.36     0.01
arrabattapp man
  • 383
  • 1
  • 3
  • 11
  • Please show a sample of your dataframe in the question. –  Nov 25 '21 at 22:17
  • `df`and `df_LtC` are referring to the same thing. hence when you change one you change the other as well. try `df=df_LtC.__deepcopy__()` – Tranbi Nov 25 '21 at 22:23

1 Answers1

0

We should assign a copy of df_LtC to df to avoid such a behavior :

df = df_LtC.copy()
tlentali
  • 3,407
  • 2
  • 14
  • 21