简体   繁体   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?

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.

相关问题 如何获取1列值并根据布尔标志列将其中一些值放入新列? - How do I take 1 column of values and put some of those values in a new column based on a boolean flag column? 如何创建一个新列,该列是在Pandas Dataframe中相似值分组在一起的另一列的最大值? - How do I create a new column which is the max of another column where similar values grouped together in Pandas Dataframe? 如何按列值过滤和定义新的 dataframe? - How do I filter and define a new dataframe by column value? 如何使用 Python 查看数据框中一列的值并根据这些结果在另一列中附加一个新值? - How do I use Python to look at the values of one column in a dataframe and append a new value in another column based on those results? 如何基于将现有列值与值列表匹配来简洁地创建新的 dataframe 列? - how do I succinctly create a new dataframe column based on matching existing column values with list of values? 如何在数据框列中找到数组中的最大值? - How do I find the maximum value in an array within a dataframe column? 如何重构数据框以基于Column [se]值创建新的列标签,然后使用Column [value]值填充这些新列 - How can I restructure a dataframe to create new column labels based on Column[se] values and then populate those new columns with Column[value] Values 如何在 pandas dataframe 中插入重复的列,并从新列的值中删除最后 3 个数字? - How do I insert a duplicated column in a pandas dataframe with the last 3 numbers removed from the values of the new column? 在 pandas dataframe 中,如何根据列值过滤行,进行计算并将结果分配给新列? - In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column? 如何创建一个 function 扫描多个 dataframe 列以获取值。 如果找到这些值中的任何一个,则新列将返回给定的数字 - How do i create a function that scans multiple dataframe columns for a value. if any of those values are found the new column returns a given figure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM