Education Gender MarritalStatus
0 graduate school male married
1 university female single
2 high school female other
3 others male single
4 university male single
那你可以用pd系列应用()应用编码
例如
def enc_for_gender(x):
if x == 'male':
return 1
return 2
def enc_for_education(x):
if x == 'graduate school':
return 1
elif x == 'university':
return 2
elif x == 'high school':
return 3
return 4
data['Gender'].apply(enc_for_gender)
假设你有这样的“数据”:
那你可以用pd系列应用()应用编码 例如
结果:
教育也一样
结果:
其他人也一样
只需将字典用作键值:
建议您使用字符串作为dict中的键,如果可能的话,您可以使用like
或者算作字符串
相关问题 更多 >
编程相关推荐