Label encoding across multiple columns in scikit-learn

Python Pandas | Label Encoding: Learn about the label encoding across multiple columns in scikit-learn.
Submitted by Pranit Sharma, on May 23, 2022

Label Encoding is the process of converting the labels into a number format so as to make them available to the machine in a machine-readable form. Machine learning algorithms can then decide in a better way how those labels must be operated.

Here, we will use sklearn library, which is used for applying machine learning process, usually the algorithms used for training the model and testing the model falls under sklearn library.

We will now understand with the help of an example that how we can do label encoding across multiple columns in sklearn.

For this purpose, we will import preprocessing function from sklearn library which will use Labelencoder method in order to achieve label encoding.

Let us understand with help of an example,

# Importing pandas package
import pandas as pd

# Importing Sklearn library
import sklearn

from sklearn import preprocessing

# Creating a dictionary
    "Marriage Status":[0,1,1,0,1,0,0,1,1,1,0,1,0]

# Now we will create DataFrame

# Encoding all the columns

# Viewing the created DataFrame
print("Created DataFrame:\n")


Example: Label encoding across multiple columns

Python Pandas Programs »


What's New (MCQs)

Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.