Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Dataframe keep two different df

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      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

>Solution :

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

df = df_LtC.copy()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading