[英]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:假设我有一个 dataframe,如下所示:
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:我想要的是 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.但是,这不会将值插入到 Age 列之前的 null 值中,False 值读取为 null 而不是 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?
制作两列新列是否更容易,一列是估算的年龄,另一列是 Boolean 标记? How would I do that?
我该怎么做? Any help would be appreciated.
任何帮助,将不胜感激。
Let us do isna
and assign让我们做
isna
并分配
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.