Reputation: 67
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?
Upvotes: 2
Views: 62
Reputation: 9826
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()
Upvotes: 0
Reputation: 36
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
Upvotes: 1
Reputation: 37847
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())
Upvotes: 0