I have this program for demonstration:
import pandas as pd
d = {'foo':[100, 111, 222],
'bar':[333, 444, 555]}
df = pd.DataFrame(d)
list = [333,444]
dferg = df.loc[df.bar.isin(list)]
dferg['test'] = 123
I get the warning:
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/indexing.html#indexing-view-versus-copy
dferg['test'] = 123
when i change:
dferg = df.loc[df.bar.isin(list)]
to
dferg = df.loc[df.bar.isin(list)].copy()
there is no more warning. But is this the best way?