2

i am using the answer from Pandas SettingWithCopyWarning in my script but it returned me 'SettingWithCopyWarning', may i know how should i fix it?

attemped1: dff['changed'] = dff.col1.ne(dff.col1.shift(1)) attemped2: dff.loc[:, 'changed] = dff.col1.ne(dff.col1.shift(1))

user466130
  • 357
  • 7
  • 19

1 Answers1

3

Your code is correct, problem is in some line above.

I guess you filter your DataFrame and solution is add copy:

dff = df[df['col2'] == 1].copy()
dff['changed'] = dff.col1.ne(dff.col1.shift(1))

If you modify values in dff later you will find that the modifications do not propagate back to the original data (df), and that Pandas does warning.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • is it true that without copy() will modify original dataframe? i don't see the effect in the test case below? df = pd.DataFrame({'col1':['A', 'B', 'C'], 'col2':[111,222,333], 'col3':['aaa', 'bbb', 'aaa']}) dff = df[df.col3 == 'aaa'] dff.loc[0, 'col3'] = 'zzz' – user466130 Aug 11 '18 at 04:46
  • @user466130 - Give me a sec. – jezrael Aug 11 '18 at 04:53
  • 1
    @user466130 - No, there is no modification in original df. It is warning, because possible [chaining indexing](http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy). Also check [this](https://stackoverflow.com/a/39983664/2901002) for explanation with sample data. – jezrael Aug 11 '18 at 05:07
  • @user466130 - Also there are another way for turn off this warning, [`pd.options.mode.chained_assignment = None`](https://stackoverflow.com/a/45110013/2901002) – jezrael Aug 11 '18 at 05:14