Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I input an array of values into a column of a dataframe where the value is null and make a new boolean column that marks this?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading