我有一个数据框看起来像这样
survived pclass sex age sibsp parch fare embarked
0 1 1 female 29.0000 0 0 211.3375 S
1 1 1 male 0.9167 1 2 151.5500 S
2 0 1 female 2.0000 1 2 151.5500 S
3 0 1 male 30.0000 1 2 151.5500 S
4 0 1 female 25.0000 1 2 151.5500 S
我想将 'sex' 转换为 0, 1 编码并使用 isnull 检查列中没有 NA
但是,在这一行我收到 ValueError: Cannot conversion non-finite value (NA or inf) to integer
df['sex'] = df['sex'].map({'female':0, 'male':1}).astype(int)
有什么建议 ?谢谢 !
Use np.where
Ex:
import numpy as np
df['sex'] = np.where(df['sex'] == 'female', 0, 1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)