Skip to content Skip to sidebar Skip to footer

Copy Pandas DataFrame Using '=' Trick

I have two pandas DataFrames, sdm. I wanted to create a copy of that DataFrame and work on that and later, I want to create another copy from sdm and work on different analysis. Ho

Solution 1:

What python does is passing by reference. Try this:

new_df = sdm.copy()

I think you should have search more, I am sure there will be lots of questions on this topic!


Solution 2:

you need to use new_df = sdm.copy() instead which is described here in the official documentation. new_df = sdm doesn't work because this assignement operation performs a copy by reference and not by value which means in nutshell, both new_df and sdm will reference the same data in memory.


Post a Comment for "Copy Pandas DataFrame Using '=' Trick"