0

I have something like this,

df1 = ...
df1['NEW_COLUMN'] = df1['SOME_COLUMN'].apply(lambda x: ...)

Although this works and I get the column 'NEW_COLUMN' added to the dataframe, I get this following annying warning. Why? And what is the solution?

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • Is this helping? https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – xcen Feb 13 '20 at 12:48
  • Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Celius Stingher Feb 13 '20 at 12:50
  • Well, I just want some easy solution to this problem, rather than going into the intrincacies of dataframes, like the answers on those post do. – MetallicPriest Feb 13 '20 at 12:50

2 Answers2

1

If you simply want to avoid getting warned, you can set it in pandas options. If you understand why the warning is, and why is it happening then you can simply ignore it by adding this after importing pandas:

pd.options.mode.chained_assignment = None

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
1

Add copy() to avoid getting this warning

df = pd.DataFrame({"Value" : [0.12,0.22,0.32,0.11,0.54,0.55,0.98]})
df['Category'] = df.Value.apply(lambda x: 'Neg' if x < 0.5 else 'Pos').copy()
ManojK
  • 1,570
  • 2
  • 9
  • 17
  • 1
    It is from Pandas docs https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html should not make much difference. – ManojK Feb 13 '20 at 14:06