使用 .fillna 将一列中的 NA 值替换为另一列中的值

Replacing the NA values from one column with the values from another column using .fillna

我尝试将 NA 替换为国家名称下的值,以及主要风险国家和国家 ISO2Code 列下的值,以及主要风险国家和 ISO2 风险主要国家代码中的值,但是它似乎不起作用。我在这里做错了什么?

port_risk_coverage['Country Name'].fillna(port_risk_coverage['Primary Country of Risk'], inplace=True)
port_risk_coverage['Country ISO2Code'].fillna(port_risk_coverage['ISO2 Code of Primary Country of Risk'], inplace=True)

NAs remain after running the above code

您的列 "Country Name""Country ISO2Code" 似乎实际上不包含要填充的 NaN 值,而是空字符串 ""。先试试这个片段,然后你可以重新运行你的代码,它应该可以工作:

import numpy as np

# Replace empty string for NaN throughout entire dataframe
port_risk_coverage = port_risk_coverage.replace("", np.nan)

port_risk_coverage['Country Name'].fillna(port_risk_coverage['Primary Country of Risk'], inplace=True)
port_risk_coverage['Country ISO2Code'].fillna(port_risk_coverage['Country ISO2Code'], inplace=True)