![](/img/trans.png)
[英]How do I take 1 column of values and put some of those values in a new column based on a boolean flag column?
[英]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.