繁体   English   中英

如何将值数组输入到 dataframe 的列中,其中值为 null 并创建一个新的 boolean 列来标记它?

[英]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?

假设我有一个 dataframe,如下所示:

Patient_ID     Age     Weight
1               27      145
2               NaN     216
3               NaN     107
4               51      156
5               NaN     201

我也有这个数组:

array([26,64,71])

我想要的是 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

我不知道该怎么做。

我尝试了列表理解:

df.loc[df[‘Age’].isna(), ‘imputed’] = True

但是,这不会将值插入到 Age 列之前的 null 值中,False 值读取为 null 而不是 False。

我试过阅读类似的问题,但找不到任何相关的内容。 制作两列新列是否更容易,一列是估算的年龄,另一列是 Boolean 标记? 我该怎么做? 任何帮助,将不胜感激。

让我们做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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM