Medical Data Visualizer

Hello again, this is a Freecodecamp challenge, this time getting some unexpected output.

I am in the step where need to normalize the data by making 0 always good and 1 always bad. If the value of cholesterol or gluc is 1, make the value 0. If the value is more than 1, make the value 1.

The output for gluc, that is gluc1, is working properly showing 0 or 1 accordingly, although with cholesterol is showing me the 1 correctly, but instead showing me 0s, is displaying Nans.

Have been working around it but cant get it done so far, please help

df.loc[df['gluc'] <= 1, 'gluc1'] = 0
df.loc[df['gluc'] > 1, 'gluc1'] = 1
df.loc[df['cholesterol'] <= 1, 'cholesterol'] = 0
df.loc[df['cholesterol'] > 1, 'cholesterol1'] = 1
print(df)

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 (Ecosia android@101.0.4951.41)

Do you want to append a “1” to the end of the column names? Some have and some don’t.

What happens if you remove those?

Other that this, (which might be part of your workaround? code looks sound. If I paste it into a working solution it’s fine (without the 1s).

df.loc[df['cholesterol'] <= 1, 'cholesterol'] = 0
df.loc[df['cholesterol'] > 1, 'cholesterol1'] = 1

This is creating NaN because you are creating a new column (colesteral1) but you have no case for =1, so =1 becomes Nan. >1 is populated correctly with 1s.

1 Like

As far as i remember if i left the same name, it would overwrite the values and displaying Nan when cholesterol was 1, instead of 0

Is so strange, i just turned of the computer and tried again with the same code you wrote, and is working just fine… don’t know, i may have messed up, as well this computer software has some incompatibilities… Thanks!

1 Like

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