python - chained assignment - receive SettingWithCopyWarning but still changing the original df?

Solution 1:

You should not try to do what you call chained assignments. Pandas documentation states that it is unspecified whether you get a view (and change the original value) or a copy (and do not). AFAIK, it depends on implementation details and on the internals on Pandas optimization code.

That being said the observed behavious is no surprise. In a Pandas DataFrame, data is stored in numpy arrays by columns. So when you access first by column (your second code), Pandas can easily give you access to the underlying numpy array as a Series. But when you access first by row (your first code) Pandas has to build a new Series, and only give you a copy of the original data.

Nevertheless, the only reliable way is to use loc:

jb.loc[jb.Weight == 75, 'Height'] = 9