I have a problem with my dataset.
Here are some values of my dataset. [1]:
CodePudding user response:
You can use boolean mask to change it. Don't use loop in pandas.
mask = (heart_disease_1.Age<=35)&(heart_disease_1.Cholesterol==0)
heart_disease_1.loc[mask, "Cholesterol"] = 194
CodePudding user response:
I believe this is an error in your loop, you don't want to do [for i in list.age], you want [for i in list], if i.age <=35 ,do replace if that makes sense.
for i in heard_disease_1:
if i.Age <= 35:
i.Cholesterol.replace(0, 194)
I believe that should get you to where you are trying to go. Let me know if it works. Also, you may need to replace i.Cholesterol
with i.<whatever the column name is>
.
Hopefully that helps,
~X
CodePudding user response:
Avoid using loops in pandas, you can use boolean masks as indexes inside assignments, then this becomes a one-liner:
hd.loc[(hd['Cholesterol']==0) & (hd['Age']<=35), 'Cholesterol'] = 194
Note that we must parenthesise the subexpressions (hd['Cholesterol']==0)
and (hd['Age']<=35)
to avoid operator precedence messing us up.