我想基于熊猫的其他列来填充缺失值。 这是我的桌子:
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
但是它失败了,我尝试了很多方法,但没有得到期望的结果。 希望收到大家的来信。
答案 0 :(得分:0)
我相信您仅在过滤的行中需要map
和dictionary
:
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'}))