I use pandas replace function to replace a value. Please see the code below:
import pandas as pd
d = {'color' : pd.Series(['white', 'blue', 'orange']),
'second_color': pd.Series(['white', 'black', 'blue']),
'value' : pd.Series([1., 2., 3.])}
df1 = pd.DataFrame(d)
print(df1)
df = df1
df['color'] = df['color'].replace('white','red')
print(df1)
print(df)
I intend to change a value in df
, but why is the same value in df1
also changed?
The code below is ok.
df=df.replace('white','red')