根据其他列熊猫填充缺失值

时间:2018-10-17 11:39:54

标签: python pandas data-science

我想基于熊猫的其他列来填充缺失值。 这是我的桌子:

Unable to init server: Could not connect: Connection refused

** (abiword:6477): WARNING **: clutter failed 0, get a life.
Unable to init server: Could not connect: Connection refused

如果性别是男性,我要填写已婚字段的缺失值->已婚是,否则已婚是:

Gender     Married
Male       Yes
Male       Yes
Female     No
Female     No
Male       NaN
Female     NaN

但是它失败了,我尝试了很多方法,但没有得到期望的结果。 希望收到大家的来信。

1 个答案:

答案 0 :(得分:0)

我相信您仅在过滤的行中需要mapdictionary

mask = df['Married'].isnull()
df.loc[mask, 'Married'] = df.loc[mask, 'Gender'].map({'Male':'Yes', 'Female':'No'})
print (df)
   Gender Married
0    Male     Yes
1    Male     Yes
2  Female      No
3  Female      No
4    Male     Yes
5  Female      No

使用numpy.where的另一种解决方案:

mask = df['Married'].isnull()
df.loc[mask, 'Married'] = np.where(df.loc[mask, 'Gender']  == 'Male', 'Yes','No')
print (df)
   Gender Married
0    Male     Yes
1    Male     Yes
2  Female      No
3  Female      No
4    Male     Yes
5  Female      No

fillna映射为Series的另一种解决方案:

df['Married'] = df['Married'].fillna(df['Gender'].map({'Male':'Yes', 'Female':'No'}))