0

I have a dataframe df. Now, I took a backup of this df using:

df_backup = df

Later on in my code, I deleted few records from the original df using:

df.drop(df.index[indexes], inplace = True)

these rows gets deleted from the backup as well. It looks like df_backup is just a copy of df. How do I decouple both?

If I change anything to df, it shouldn't affect df_backup.

1 Answer 1

1

you can decouple them by making an actual copy (a copy is a separate object)

df_backup = df.copy()

as Anthony Sottile pointed out, you were creating another reference to your original dataframe rather than creating a new object. Which means you could change either your df or df_backup and both would show that change. He also suggested a good link to help understand this

Sign up to request clarification or add additional context in comments.

1 Comment

might be worth adding that df_backup = df does not create a copy (since python is a referential language) and just makes two references pointing to the same object. OP seems confused about this :) perhaps stackoverflow.com/questions/986006/… might clear some stuff up too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.