pandas: 'A value is trying to be set on a copy of a slice from a DataFrame'
@devsingh05▲ 5 net votes
Chained indexing like df[df.x > 0]['y'] = 1 may modify a temporary copy, so the original is unchanged. Use .loc in one step: df.loc[df.x > 0, 'y'] = 1. When you deliberately want a separate subset, take an explicit copy (sub = df[df.x > 0].copy()) before modifying it. Newer pandas copy-on-write behaviour makes chained assignment never update the original.