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