How to properly copy a pandas dataframe into another variable?
toking, normally if you say data2=data1 it passes to data2 the reference (a pointer) to data1. Thus when you make changes to data2 you really making them on data1. In your case, however, you already allocated memory to your data2, thus data1[['Country/Region','Province/State','Lat','Long']]=confirmed[['Country/Region','Province/State','Lat','Long']] code copies the content to the allocated memory. But it is generally a good practice to use .copy(deep=True) if you want to avoid confusion. Cheers
Someone was reviewing my code and told me to use the .copy() function when copying a pandas dataframe into another variable. My code was like this:
data1 = pd.DataFrame()
data1[['Country/Region','Province/State','Lat','Long']]=confirmed[['Country/Region','Province/State','Lat','Long']]
I copied the dataframe into another dataframe like this:
data2 = data1 and was working alright. Alright means, data1 is not being changed when I work on the data2. So, I am guessing it is ok. I am using Jupyter Notebook.
Should I stick to using it in the future or use .copy()?