2

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?

Uqhah
  • 57
  • 11

3 Answers3

1

df[['a','b','d']] is a like view of original dataframe...trying to set values in a view may or may not work everytime

do it seperately

df['a']=df['a'].mean()
df['b']=df['b'].mean()
df['d']=df['d'].mean()

its doesn't make much difference in performance

jsn
  • 36
  • 3
0

Are you sure you're getting the warning at that statement/line ?

Anyways, In a Pandorable way and to reduce visible noise, I would do :

cols = ["a", "b", "d"]

df[cols] = df[cols].sub(df[cols].mean())
Timeless
  • 22,580
  • 4
  • 12
  • 30
0

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()
Hamzah
  • 8,175
  • 3
  • 19
  • 43