user308827 user308827 - 1 year ago 113
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?

Answer Source

Perhaps this:

df_n_years = df_n_years.rename(columns=lambda x: 'VAL_' + str(x))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download