0

I was working with a dataframe, and to avoid editing the original dataframe I wanted to create a copy to work with. However, my original dataframe seems to be overwritten when editing my copied dataframe. I can't work out why my code would do this. Simplified version of code included below.

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(0,100,size=(3, 3)), columns=['col one', 'col two', 'col three'])
df_new = df
df_new.columns = ['_'.join(c.split()) for c in df_new.columns]
print(df)

As far as I was aware, after creating df, I haven't edited it further at any point. However, my column names of df now have underscores between the words. However, I just wanted to do this to my copy df_new.

0 Answers0