0

I am getting an error in python for the below piece of code. I even added the .loc arguement after reading further about this error but still the error pops up.

thrsh=calib1[calib1.VARIABLE.str.contains("Threshold")]
thrsh.loc[:,'Threshold_Price'] = thrsh['VARIABLE'].str.split(' ').str[0]# problem statement

Its a simple operation wherein I am trying to add a new variable - Threshold_Price. Threshold price would have value 1_29 if column "Variable" has value - 1_29 Regular Price Threshold

Error Msg: C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:362: 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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj[key] = _infer_fill_value(value) C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:543: 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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj[item] = s

  • it is because `thrsh` is a slice of `calib1`. try with `thrsh=calib1[calib1.VARIABLE.str.contains("Threshold")].copy()` – Ben.T Aug 26 '20 at 12:03
  • I am unable to mark this as accepted answer. Not sure what's wrong. Please help and thanks for answering my question. Really appreciate it! – Pawan Tolani Aug 26 '20 at 14:44
  • It's because it is a comment not an answer :) your question is kind of a common misunderstanding of the SettingWithCopyWarning problem. I will close it with a dup and think the dup is worth to read through :) – Ben.T Aug 26 '20 at 15:07
  • That said, the answer that covers the most is the [one from cs95](https://stackoverflow.com/a/53954986/9274732) (not the accepted answer, which is good too) – Ben.T Aug 26 '20 at 15:08

0 Answers0