26

In pandas data frame, I'm trying to map df['old_column'], apply user defined function f for each row and create a new column.

df['new_column'] = df['old_column'].map(lambda x: f(x))

This will give out "SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame." error.

I tried the following:

df.loc[:, 'new_column'] = df['old_column'].map(lambda x: f(x))

which doesn't help. What can I do?

Ik-Hwan Kim
  • 303
  • 1
  • 3
  • 7

1 Answers1

19

A SettingWithCopy warning is raised for certain operations in pandas which may not have the expected result because they may be acting on copies rather than the original datasets. Unfortunately there is no easy way for pandas itself to tell whether or not a particular call will or won't do this, so this warning tends to be raised in many, many cases where (from my perspective as a user) nothing is actually amiss.

Both of your method calls are fine. If you want to get rid of the warning entirely, you can specify:

pd.options.mode.chained_assignment = None

See this StackOverflow Q&A for more information on this.

Community
  • 1
  • 1
Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57
  • 2
    I suggest putting a `pd.options.mode.chained_assignment = 'warn'` after the triggering statement, to catch possible true positives of this warning in your subsequent code. – Philipp Feb 12 '21 at 14:36