0

I am creating a new column ("nf") in a dataframe which contains the difference of subsecquent lines of the "cf" column.

I use the simple code:

gdf["nf"]=gdf["cf"].diff()

This seems to work fine, but I keep the (apparently popular) 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

I'd happily use loc, but am completely unsure how to do that. I was looking for solutions but could not find any example that used diff().

How do I fix this?

user1583209
  • 1,637
  • 3
  • 24
  • 42
  • 2
    When creating `gdf` use `.copy()`, the problem is not in creating your column, it's when creating `gdf` which is a copy/subset of another dataframe. – Erfan May 12 '20 at 22:57
  • 1
    Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Trenton McKinney May 12 '20 at 23:20
  • Flagged as a duplicate - as Erfan mentioned this issue doesn't have anything to do with .diff but instead with how you created gdf, and other questions on SettingWithCopyWarning should get you where you need to go. – Michael Delgado May 13 '20 at 00:54
  • Erfan's answer (with .copy()) solved my problem. Thanks for that. However I did not see this answer in the suggest duplicate answer. So I don't think it is actually a duplicate. – user1583209 May 13 '20 at 09:55

0 Answers0