0

I'm getting SettingWithCopyWarning on the following code although I use .loc.

import pandas as pd
df = pd.DataFrame([10,10,11,11,12,12],columns=['num'])
mask = (df['num'] % 2) == 0
df = df.drop_duplicates(keep='last')
df.loc[mask, 'num'] = 7

I think it might be because I create the mask and after drop the duplications, but I can't change the order in my case.

Any other idea to solve it?

pnina
  • 119
  • 1
  • 2
  • 10
  • Use `df = df.drop_duplicates(keep='last').copy()` – jezrael Nov 06 '20 at 09:18
  • It is necessary, because `drop_duplicates` also filter rows. – jezrael Nov 06 '20 at 09:19
  • @jezrael, Thanks! It solve the problem. Can you please explain me what do you mean by "drop_duplicates also filter rows"? how can I find the differences between `drop_duplicates` and `drop_duplicates .copy()` – pnina Nov 08 '20 at 08:16
  • For bette see if use `df1 = df.drop_duplicates(keep='last')` - If you modify values in `df1` later you will find that the modifications do not propagate back to the original data (`df`), and that Pandas does warning. – jezrael Nov 09 '20 at 06:10
  • the code didn't raise the warning on my machine, and i think it shouldn't with your code example shown here. `drop_duplicates` doesn't return a copy rather its a view, so if you modify it, pandas will raise the SettingWithCopyWarning, however, since you are overwirting the variable `df`, `df` now is a completely new object (if you check with `id()`, it has different value), which means it should not give you the warning. – Sam Nov 14 '20 at 00:30

0 Answers0