0

When I add a new "NaN" column ("C") to a dataframe (say df) to fill it later it will be added to other similar dataframes (here: sto) which is not my case. So how should I avoid this and remain the other dataframes unchanged?

import numpy as np  
import pandas as pd
df=pd.DataFrame({"A":[1,2,3],"B":[3,4,9]})
display(df)

enter image description here

sto=df
df.loc[:,"C"]=np.nan
display(df)

enter image description here

display(sto)

enter image description here

yasharov
  • 113
  • 10

1 Answers1

0

sto and df are different variables pointing to the same object. You should sto=df.copy() to alter sto without changing df.

You can see that sto is df returns True when using sto=df but False when using df.copy()

politinsa
  • 3,480
  • 1
  • 11
  • 36
  • Does this happen when using "=" in other objects such as lists, dictionaries, np.arrays and we should use ".copy()" or other similar methods for them or it is just relating to pandas dataframes? – yasharov Mar 30 '21 at 10:18
  • See https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference and try by yourself – politinsa Mar 30 '21 at 12:18