我认为你需要boolean indexing
与notnull
创建面膜:
df_features[df_features['sexo'].notnull()]
看来你需要:
df_features[(df_features['sexo'].notnull()) & (df_features['age'] != 'NA')]
样品:
df_features = pd.DataFrame({'sexo':[np.nan,2,3],
'age':['10','20','NA']})
print (df_features)
age sexo
0 10 NaN
1 20 2.0
2 NA 3.0
a = df_features[(df_features['sexo'].notnull()) & (df_features['age'] != 'NA')]
print (a)
age sexo
1 20 2.0
但似乎你与01共谋值不是数字,而是字符串。
如果需要一些列转换为数字,尝试to_numeric
,参数errors='coerce'
方式转变,不能再见解析为数字的所有值NaN
:
df_features = pd.DataFrame({'sexo':[np.nan,2,3],
'age':['10','20','NA']})
print (df_features)
age sexo
0 10 NaN
1 20 2.0
2 NA 3.0
df_features['age'] = pd.to_numeric(df_features['age'], errors='coerce')
print (df_features)
age sexo
0 10.0 NaN
1 20.0 2.0
2 NaN 3.0
a = df_features[(df_features['sexo'].notnull()) & (df_features['age'].notnull())]
print (a)
age sexo
1 20.0 2.0
呀,我找到NA是字符串和数据他们是' NA'和'NA'。 – yanachen