w3resource

Applying label encoding to categorical data using Pandas

Pandas: Machine Learning Integration Exercise-5 with Solution

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.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/pandas/pandas-apply-label-encoding-to-categorical-data.php