Let's say I have a dataframe as follows:
Patient_ID Age Weight
1 27 145
2 NaN 216
3 NaN 107
4 51 156
5 NaN 201
I also have this array:
array([26,64,71])
What I would like to have is the dataframe:
Patient_ID Age Weight Age_Predicted
1 27 145 False
2 26 216 True
3 64 107 True
4 51 156 False
5 71 201 True
I am not sure how to do this though.
I tried a list comprehension:
df.loc[df[‘Age’].isna(), ‘imputed’] = True
However, this doesn't insert the values into the previously null values of the Age column, and the False values read as null and not False.
I have tried reading similar questions, but can't find anything that relates. Would it be easier to make two new columns, one with the Ages imputed and one with the Boolean marker? How would I do that? Any help would be appreciated.
CodePudding user response:
Let us do isna
and assign
df['Age_Predicted'] = df['Age'].isna()
df.loc[df['Age_Predicted'],'Age'] = a
df
Out[323]:
Patient_ID Age Weight Age_Predicted
0 1 27.0 145 False
1 2 26.0 216 True
2 3 64.0 107 True
3 4 51.0 156 False
4 5 71.0 201 True