Applying label encoding to categorical data using Pandas
5. Converting Categorical Variables into Numerical Values Using Label Encoding
Write a Pandas program that converts categorical variables into numerical values using label.
This exercise shows how to convert categorical variables into numerical values using label encoding for machine learning models.
Sample Solution :
Code :
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# Load the dataset
df = pd.read_csv('data.csv')
# Initialize the LabelEncoder
le = LabelEncoder()
# Apply label encoding to the 'Gender' column
df['Gender'] = le.fit_transform(df['Gender'])
# Output the encoded dataset
print(df)
Output:
ID Name Age Gender Salary Target 0 1 Sara 25.0 0 50000.0 0 1 2 Ophrah 30.0 1 60000.0 1 2 3 Torben 22.0 1 70000.0 0 3 4 Masaharu 35.0 1 80000.0 1 4 5 Kaya NaN 0 55000.0 0 5 6 Abaddon 29.0 1 NaN 1
Explanation:
- Loaded the dataset using Pandas.
- Initialized the LabelEncoder from Scikit-learn.
- Applied label encoding to the 'Gender' column, converting categorical values into numerical form.
- Displayed the encoded dataset.
For more Practice: Solve these Related Problems:
- Write a Pandas program to label encode categorical variables and verify the mapping between categories and numbers.
- Write a Pandas program to apply label encoding to a DataFrame with mixed data types and handle unseen labels.
- Write a Pandas program to encode categorical variables using label encoding and then compare the encoded values across multiple columns.
- Write a Pandas program to perform label encoding on a column and check for consistency of the encoding after splitting the dataset.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.