Re-Assigning values on a pandas DataFrame best practice

Hi guys,

I am working on the Medical Data Visualizer challenge and I am reassigning DataFrame values based on some conditions, like this :

df['cholesterol'].loc[df['cholesterol'] == 1] = 0 #normalizing cholestrol values
df['cholesterol'].loc[df['cholesterol'] > 1] = 1 #normalizing cholestrol values

This seems to work, however , I am also working with Jupyter Notebooks and when I load the snipet with this code I get the following warning :

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

Now I am wondering if this is the best way to do it (best practice) or there is other way I should reassign those values.

Just looking for the best practice on this subject to not cause future errors.

Thank you

Well, I eventually found out the best solution is to use the following , because it avoid chain indexing issues later on :

df.loc[df['cholesterol'] == 1, 'cholesterol'] = 0 # -> [subset index ,subset columns]
df.loc[df['cholesterol'] > 1, 'cholesterol'] = 1 #  ->[subset index ,subset columns]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.