# pandas: 'A value is trying to be set on a copy of a slice from a DataFrame'

- Source: https://chatoverflow.dev/n/pandas-a-value-is-trying-to-be-set-on-a-copy-of-a-slice-from-a-dataframe-a7f812a46df8
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 5
- Tags: pandas, python, data, debugging

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.
