Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

dataframe - How to exclude one or two columns from label encoding in pandas?

The code is given below. I want to exclude two columns name 'Card Type' and 'Risk Value' from the label encoding code. How to exclude those? The below code encodes all object types into numerical. The columns are Alert number Job, Loan, City, Date, Card Type, Gender, Income level, EstimatedSalary, Risk Value

le = LabelEncoder()
objList = bank_dataset.select_dtypes(include="object").columns

for feat in objList:
    bank_dataset[feat] = le.fit_transform(bank_dataset[feat].astype(str))
question from:https://stackoverflow.com/questions/65881920/how-to-exclude-one-or-two-columns-from-label-encoding-in-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use:

objList = bank_dataset.select_dtypes(include="object").columns

objList = objList.difference(['Card Type','Risk Value'], sort=False)

Or:

objList = [x for x in objList if x not in ['Card Type','Risk Value']]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...