I have got this 'change on copy' warning during a column rename with df1.rename()
:
import pandas as pd
df = pd.DataFrame({'a': (1,2,3),
'b': (4,5,6),
'c': (7,8,9)})
df1 = df[['a', 'b']]
#df1.columns = ('x', 'y') # Displays nothing
df1.rename(columns={df1.columns[0]: 'x', df1.columns[1]: 'y'}, inplace=True) # Displays the warning
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
However I don't get this warning when I completely change the column headers using df1.columns = ()
I don't get the rationale for the difference, I assumed both operations are equivalent, for example no difference is mentioned in answers to this question.
Note I'm not asking how to work on a copy of the original data, which indeed would suppress the warning. I'm asking about the difference between the two methods to rename series.
CodePudding user response:
Use .loc
to make a copy:
df1 = df.loc[:, ['a', 'b']]
df1.rename(columns={df1.columns[0]: 'x', df1.columns[1]: 'y'}, inplace=True)
To know more: Returning a view versus a copy
CodePudding user response:
Use .copy() or .loc to make a copy:
df1=df[['a', 'b']].copy()
# or
df1=df.loc[:,[df.columns[0],df.columns[1]]