使用 For 循环填充选定数据帧列的 NaN 值
我有一个患者生命体征(HR、O2Sat、Temp、SBP、DBP、Resp)的数据框,其值为 NaN。我使用以下代码根据患者 ID (P_ID) 列在个别患者中填写了 NaN:
m['HR'] = m['HR'].fillna(m.groupby('P_ID')['HR'].transform('mean'))
m['O2Sat'] = m['O2Sat'].fillna(m.groupby('P_ID')['O2Sat'].transform('mean'))
m['Temp'] = m['Temp'].fillna(m.groupby('P_ID')['Temp'].transform('mean'))
m['SBP'] = m['SBP'].fillna(m.groupby('P_ID')['SBP'].transform('mean'))
m['DBP'] = m['DBP'].fillna(m.groupby('P_ID')['DBP'].transform('mean'))
m['Resp'] = m['Resp'].fillna(m.groupby('P_ID')['Resp'].transform('mean'))
它工作得很好。然而,它的代码很多。无论如何,我是否使用 for 循环 仅在重要列中填充 NaN 值?因为还有一些没有 NaN 值的列。谢谢。
I have a dataframe of patients Vital signs (HR, O2Sat, Temp, SBP, DBP, Resp) with NaN values. I filled NaN in individual patient based on Patient ID (P_ID) column using the code:
m['HR'] = m['HR'].fillna(m.groupby('P_ID')['HR'].transform('mean'))
m['O2Sat'] = m['O2Sat'].fillna(m.groupby('P_ID')['O2Sat'].transform('mean'))
m['Temp'] = m['Temp'].fillna(m.groupby('P_ID')['Temp'].transform('mean'))
m['SBP'] = m['SBP'].fillna(m.groupby('P_ID')['SBP'].transform('mean'))
m['DBP'] = m['DBP'].fillna(m.groupby('P_ID')['DBP'].transform('mean'))
m['Resp'] = m['Resp'].fillna(m.groupby('P_ID')['Resp'].transform('mean'))
It worked perfectly. However it is a lot of code. Is there anyway I use for loop to fill NaN values in only the vital columns? As there are some more columns without NaN values. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以使用循环
Yes, you can use a loop
为了避免不必要的行和冗长的脚本,您可以使用数据帧的
columns
属性。但请注意,由于您提到了 vital 列,因此可能有一些列您不希望执行以下代码:In the interest of avoiding unnecessary lines and lengthy scripts, you can use the
columns
attribute of the dataframe. But note that, since you mentioned vital columns, there could be a few columns that you don't want to do the following code.: