How to encode ordinal categorical features in Python?

How to encode ordinal categorical features in Python?

How to encode ordinal categorical features in Python?

This recipe helps you encode ordinal categorical features in Python

Recipe Objective

Have you ever tried to encode ordinal categorical features by making a simple function which is quiet easy to understand and change.

So this is the recipe on how we can encode ordinal categorical features in Python.

Step 1 - Import the library

import pandas as pd

We have imported pandas which will be needed for the dataset.

Step 2 - Setting up the Data

We have created a dataframe with one feature "score" with categorical variables "Low", "Medium" and "High". df = pd.DataFrame({"Score": ["Low", "Low", "Medium", "Medium", "High", "Low", "Medium","High", "Low"]}) print(df)

Step 3 - Encoding variable

We have created an object scale_mapper in which we have passed the encoding parameter i.e putting numerical values instead of categorical variable. We have made a feature scale in which there will be numerical encoded values. scale_mapper = {"Low":1, "Medium":2, "High":3} df["Scale"] = df["Score"].replace(scale_mapper) print(df) So the output comes as:

0     Low
1     Low
2  Medium
3  Medium
4    High
5     Low
6  Medium
7    High
8     Low

    Score  Scale
0     Low      1
1     Low      1
2  Medium      2
3  Medium      2
4    High      3
5     Low      1
6  Medium      2
7    High      3
8     Low      1

Download Materials

Relevant Projects

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.