user308827 user308827 - 11 months ago 60
Python Question

Avoiding setting with copy warning

df_n_years = df.iloc[:, -self.num_years - 1:-1]
df_n_years.rename(columns=lambda x: 'VAL_' + str(x), inplace=True) # results in SettingWithCopyWarning

I am extracting the last few columns from a dataframe and then renaming them. It does result in a setting with copy warning. I understand why the warning is happening, what's the best way to avoid it?


Perhaps this:

df_n_years = df_n_years.rename(columns=lambda x: 'VAL_' + str(x))