Suppose I have a dataframe df with columns a, b, c, d and I want to subtract mean of the columns from columns a,b,d. How do I achieve the same?
I have tried df[['a','b','d']] = df[['a','b','d']] - df[['a','b','d']].mean()
but I get SettingWithCopyWarning. How do I achieve the same without the warning?
When you try to modify a slice of the dataframe directly, e.g., df[['a','b','d']], this can lead to unexpected behavior if you're not careful. Thus, this warning arises to carefully warn you that the original dataframe is being changed by doing this copying process. To suppress this warning, you can use:
mean = df[['a','b','d']].mean()
df[['a','b','d']] = df[['a','b','d']] - mean
or
df.loc[:, ['a','b','d']] = df[['a','b','d']] - df[['a','b','d']].mean()