gpt4 book ai didi

python - 如何在不影响 DataFrame 中的数字数据的情况下对分类数据进行编码?

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:11 27 4
gpt4 key购买 nike

     Loan_ID    Gender  Married Dependents  Education   ApplicantIncome
1 LP001003 Male Yes 1 Graduate 4583
2 LP001005 Male Yes 0 Graduate 3000
3 LP001006 Male Yes 0 Not Graduate 2583
4 LP001008 Male No 0 Graduate 6000
5 LP001011 Male Yes 2 Graduate 5417

How to encode 'Gender', 'Married', 'Education' columns without affecting 'Loan_ID', 'Dependents','ApplicantIncome' columns.

最佳答案

这应该可以解决您的问题。

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()

for cat_var in ['Gender', 'Married', 'Education']:
df[cat_var] = le.fit_transform(df[cat_var])

关于python - 如何在不影响 DataFrame 中的数字数据的情况下对分类数据进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58969758/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com