-1

the following piece of code exemplifies my 'problem' or rather wierd behavior of DataFrames:

display(df)
df_copy=df
df_copy.columns=['C','D']
display(df)

this outputs:

    A   B
0   1   1
1   3   3
2   2   6

    C   D
0   1   1
1   3   3
2   2   6

Why do the column names change for df, when I only changed them in df_copy?

1 Answers1

0

Use .copy() instead:

df_copy = df.copy()
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53