0

I know there are many similar questions but I can't find the exact situation I have here, specifically why it gets the 'A value is trying to be set on a copy of a slice from a DataFrame' warning.

This is my code:

df = pd.DataFrame([[1,2,1],
            [4,5,6]],
            columns = ['a','b','c'])
df_nov = df[((df.a == 1)|(df.b == 1)) & (df.c == 1)]
df_nov['total'] = df_nov.sum(axis=1)
df_nov

    a   b   c   total
 0  1   2   1   4

Which generates this warning:

/Library/Python/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:5: 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/user_guide/indexing.html#returning-a-view-versus-a-copy """

This line with the boolean mask is causing the issue:

df_nov = df[((df.a == 1)|(df.b == 1)) & (df.c == 1)]

How am I meant to write that so as to avoid this warning?

Thanks in advance

Liam McIntyre
  • 334
  • 1
  • 13
  • Use `df_nov = df[((df.a == 1)|(df.b == 1)) & (df.c == 1)].copy()` – Erfan Feb 06 '20 at 22:20
  • Thank you, Erfan. That is the answer I was looking for! I had already seen the other answer that you linked to and wasn't happy with the solution (ignore the warning). – Liam McIntyre Feb 07 '20 at 23:41
  • @Erfan - does this work because the way I did it df_nov was a view and therefore changes to df_nov would alter the original df? And with copy its a stand alone copy? – Liam McIntyre Feb 07 '20 at 23:43

0 Answers0