0

I have a pandas dataframe with a range of columns, including column "A" I want to add 3 new columns to it, and this method works:

  for sample in samples:
     df= df1.loc[df1['sample'] == sample, ]
     df.reset_index(inplace=True, drop=True)
     df["new"] = df["A"].diff()
     df["new2"] = df["new"].diff()
     df["new3"] = np.nan

However all of these operations give the following warning:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

What is the right way of assigning the new columns without the warning? I've had a look at How to deal with SettingWithCopyWarning in Pandas? but I don't understand what the issue is.

Thanks

Agustin
  • 1,458
  • 1
  • 13
  • 30
  • what is your code before `df["new"] = df["A"].diff() df["new2"] = df["new"].diff()` ? – jezrael Aug 14 '20 at 12:09
  • 1
    Problem of this error is get in some lines after problemic line, obviously filtering. So is necessary copy more code to question. – jezrael Aug 14 '20 at 12:11
  • I agree with jezrael, these 3 lines did not trigger `SettingWithCopyWarning` code before or after it triggered it, if you are working with *jupyter* run each line in a separate cell to find out which is triggering the warning(all assignment lines i.e lines where you use `=`(assignment) operator.) – Ch3steR Aug 14 '20 at 12:14
  • Updated to show the code before, pycharm quoted those 3 lines, so I assumed they were the problematic ones. – Agustin Aug 14 '20 at 12:16
  • 2
    Use `df= df1.loc[df1['sample'] == sample, ].copy()` for avoid error. – jezrael Aug 14 '20 at 12:17
  • 1
    Thanks, I should have looked at the previous lines... – Agustin Aug 14 '20 at 12:17

1 Answers1

2

As mentioned by @jezrael. Whenever you want to duplicate/subset a dataframe, use the .copy() at the end to avoid this type of warning. To understand why this happens, I would recommend the 2nd answer by "cs95" in this post: How to deal with SettingWithCopyWarning in Pandas?