๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ machine learning โ€บ ml-label-encoding-of-datasets-in-python
Label Encoding in Python - GeeksforGeeks
LabelEncoder can also encode non numeric labels (strings) as long as they are hashable. Python ยท
Published: June 11, 2026
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ ai and machine learning โ€บ label encoding in python
What is Label Encoding in Python | Great Learning
December 18, 2024 - In label encoding in python, we replace the categorical value with a numeric value between 0 and the number of classes minus 1. Learn more!
๐ŸŒ
Medium
medium.com โ€บ @kattilaxman4 โ€บ a-practical-guide-for-python-label-encoding-with-python-fb0b0e7079c5
A Practical Guide for Python: Label Encoding with Python | by Kattilaxman | Medium
October 25, 2023 - However, it is important to note that label encoding should be used with caution, especially when dealing with features with a high number of categories. The reason is that label encoding introduces ordinality into the data, which does not exist in Python.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ how to perform label encoding in python?
Label Encoding in Python Explained with Examples
December 19, 2023 - Using the label encoder in Python class from the sci-kit-learn library, we can conduct label encoding in Python.
๐ŸŒ
DataCamp
campus.datacamp.com โ€บ courses โ€บ working-with-categorical-data-in-python โ€บ pitfalls-and-encoding
Label encoding | Python
Label encoding is a technique that codes categorical values as integers. In Python, these codes often start at 0 and end at n - 1, where n is the number of categories. A -1 code is often used to indicate any missing values. Label encoding is used to save memory and to simplify responses when ...
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to perform label encoding in python (with example)
How to Perform Label Encoding in Python (With Example)
August 26, 2022 - One way to do this is through label encoding, which assigns each categorical value an integer value based on alphabetical order.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ label-encoding
Label Encoding in Python - A Quick Guide! - AskPython
February 16, 2023 - For example, if a dataset contains a variable โ€˜Genderโ€™ with labels โ€˜Maleโ€™ and โ€˜Femaleโ€™, then the label encoder would convert these labels into a number format and the resultant outcome would be [0,1]. Thus, by converting the labels into the integer format, the machine learning model can have a better understanding in terms of operating the dataset. Python sklearn library provides us with a pre-defined function to carry out Label Encoding on the dataset.
๐ŸŒ
Medium
medium.com โ€บ @sunnykumar1516 โ€บ what-is-label-encoding-application-of-label-encoder-in-machine-learning-and-deep-learning-models-c593669483ed
label encoding. what is label encoding. label encoding in machine learning. sklearn label encoding . python label encoding. python label encoder | Medium
January 12, 2024 - Label encoding is a process in machine learning where categorical data, represented as labels or strings, is converted into numerical format. In this encoding technique, each unique category is assigned a unique integer, effectively converting ...
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ label-encoding-in-python
Label Encoding in Python - Javatpoint
Label Encoding in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
๐ŸŒ
Spot Intelligence
spotintelligence.com โ€บ home โ€บ practical guide and tutorial to label encoding in python
Practical Guide And Tutorial To Label Encoding In Python
October 11, 2024 - In this example, we create a DataFrame with a categorical column โ€˜Categoryโ€™ and then use the pd. factorize method to perform label encoding. The encoded values are stored in a new column, โ€˜Category_encoded.โ€™ ยท See also SimHash โ€” The Ultimate Guide And How To Get Started Guide In Python
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ label-encoding-in-python
Label encoding in Python
The output shows that the values of the "Fruit" column have converted into numerical values starting from 0. The numerical values assigned are not random. Rather, label encoding is based on assigning values in alphabetical order.
๐ŸŒ
Practical Business Python
pbpython.com โ€บ categorical-encoding.html
Guide to Encoding Categorical Values in Python - Practical Business Python
Another approach to encoding categorical values is to use a technique called label encoding. Label encoding is simply converting each value in a column to a number. For example, the body_style column contains 5 different values.
๐ŸŒ
Jaro Education
jaroeducation.com โ€บ home โ€บ blog โ€บ label encoding in python
Python Implementation of Label Encoding in 2024
6 days ago - Label encoding in Python can be performed using scikit-learn library LabelEncoder class which is part of the pre-processing module. However, this module is not activated by default while using a Python interpreter or using any Python IDE such ...
๐ŸŒ
Medium
medium.com โ€บ @vtalladin06 โ€บ label-encoding-in-python-ec0bbe6f0e0f
Label Encoding in Python. Introduction: | by Tahseen Alladin | Medium
February 1, 2024 - Label encoding can be useful when ... assigning numerical values accordingly. ... In Python, scikit-learn (sklearn) provides the LabelEncoder class for label encoding....
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Label Encoding: A Beginner-Friendly Guide for Data Science (Step-by-Step) - YouTube
Are you new to Python and eager to learn label encoding? This beginner-friendly tutorial simplifies the theory behind label encoding and walks you through st...
Published: January 27, 2025
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ glossary โ€บ label-encoding
Label Encoding | Saturn Cloud
April 4, 2023 - To further perform label encoding in Python (using the scikit-learn library)
Top answer
1 of 16
609

You can easily do this though,

df.apply(LabelEncoder().fit_transform)

EDIT2:

In scikit-learn 0.20, the recommended way is

OneHotEncoder().fit_transform(df)

as the OneHotEncoder now supports string input. Applying OneHotEncoder only to certain columns is possible with the ColumnTransformer.

EDIT:

Since this original answer is over a year ago, and generated many upvotes (including a bounty), I should probably extend this further.

For inverse_transform and transform, you have to do a little bit of hack.

from collections import defaultdict
d = defaultdict(LabelEncoder)

With this, you now retain all columns LabelEncoder as dictionary.

# Encoding the variable
fit = df.apply(lambda x: d[x.name].fit_transform(x))

# Inverse the encoded
fit.apply(lambda x: d[x.name].inverse_transform(x))

# Using the dictionary to label future data
df.apply(lambda x: d[x.name].transform(x))

MOAR EDIT:

Using Neuraxle's FlattenForEach step, it's possible to do this as well to use the same LabelEncoder on all the flattened data at once:

FlattenForEach(LabelEncoder(), then_unflatten=True).fit_transform(df)

For using separate LabelEncoders depending for your columns of data, or if only some of your columns of data needs to be label-encoded and not others, then using a ColumnTransformer is a solution that allows for more control on your column selection and your LabelEncoder instances.

2 of 16
132

As mentioned by larsmans, LabelEncoder() only takes a 1-d array as an argument. That said, it is quite easy to roll your own label encoder that operates on multiple columns of your choosing, and returns a transformed dataframe. My code here is based in part on Zac Stewart's excellent blog post found here.

Creating a custom encoder involves simply creating a class that responds to the fit(), transform(), and fit_transform() methods. In your case, a good start might be something like this:

import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline

# Create some toy data in a Pandas dataframe
fruit_data = pd.DataFrame({
    'fruit':  ['apple','orange','pear','orange'],
    'color':  ['red','orange','green','green'],
    'weight': [5,6,3,4]
})

class MultiColumnLabelEncoder:
    def __init__(self,columns = None):
        self.columns = columns # array of column names to encode

    def fit(self,X,y=None):
        return self # not relevant here

    def transform(self,X):
        '''
        Transforms columns of X specified in self.columns using
        LabelEncoder(). If no columns specified, transforms all
        columns in X.
        '''
        output = X.copy()
        if self.columns is not None:
            for col in self.columns:
                output[col] = LabelEncoder().fit_transform(output[col])
        else:
            for colname,col in output.iteritems():
                output[colname] = LabelEncoder().fit_transform(col)
        return output

    def fit_transform(self,X,y=None):
        return self.fit(X,y).transform(X)

Suppose we want to encode our two categorical attributes (fruit and color), while leaving the numeric attribute weight alone. We could do this as follows:

MultiColumnLabelEncoder(columns = ['fruit','color']).fit_transform(fruit_data)

Which transforms our fruit_data dataset from

to

Passing it a dataframe consisting entirely of categorical variables and omitting the columns parameter will result in every column being encoded (which I believe is what you were originally looking for):

MultiColumnLabelEncoder().fit_transform(fruit_data.drop('weight',axis=1))

This transforms

to

.

Note that it'll probably choke when it tries to encode attributes that are already numeric (add some code to handle this if you like).

Another nice feature about this is that we can use this custom transformer in a pipeline:

encoding_pipeline = Pipeline([
    ('encoding',MultiColumnLabelEncoder(columns=['fruit','color']))
    # add more pipeline steps as needed
])
encoding_pipeline.fit_transform(fruit_data)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ machine learning โ€บ label-encoding-across-multiple-columns-in-scikit-learn
Label Encoding Across Multiple Columns in Scikit-Learn - GeeksforGeeks
July 23, 2025 - Preprocessing data is a crucial step that often involves converting categorical data into a numerical format. One of the most common techniques for this conversion is label encoding. This article delves into the intricacies of applying label encoding across multiple columns using Scikit-Learn, a popular machine learning library in Python.