‘A value is trying to be set on a copy of a slice from a DataFrame’ error while using ‘iloc’

I think you need copy:

train = df.loc[df['group'] != i].copy()
test = df.loc[df['group'] == i].copy()

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

Leave a Comment