Pandas SettingWithCopyWarning [duplicate] Pandas SettingWithCopyWarning [duplicate] python python

Pandas SettingWithCopyWarning [duplicate]


The issue here is that: df.col1[df.col1 == 10] returns a copy.

So I would say:

row_index = df.col1 == 10# then with the form .loc[row_indexer,col_indexer]df.loc[row_index, 'col1'] = 100


Agreed with Paul about 'loc' usage.

For your applymap case you should be able to do this:

cols = ['col1', 'col2', 'col3']df.loc[:, cols] = df[cols].applymap(some_function)


As suggested in the error message, you should use loc to do this:

sve2_all.loc[sve2_all['Hgtot ng/l'] > 100] = np.nan

The warning is here to stop you modifying a copy (here sve2_all[sve2_all[' Hgtot ng/l'] > 100] is potentially a copy, and if it is then any modifications would not change the original frame. It could be that it works correctly in some cases but pandas cannot guarantee it will work in all cases... use at your own risk (consider yourself warned! ;) ).