- Lab
- Data

Convert Categorical Variables into Quantitative Variables Hands-on Practice
In this lab, you'll practice encoding categorical data for machine learning using Python and Pandas. you will engage in tasks like loading datasets, applying ordinal and one-hot encoding, and manipulating data columns, developing essential data preprocessing skills.

Path Info
Table of Contents
-
Challenge
Practice Encoding Categorical Data
Jupyter Guide
To get started, open the file on the right entitled "Step 1...". You'll complete each task for Step 1 in that Jupyter Notebook file. Remember, you must run the cells
(ctrl/cmd(β) + Enter)
for each task before moving onto the next task in the Jupyter Notebook. Continue until you have completed all tasks in this step.
Practice Encoding Categorical Data
To review the concepts covered in this step, please refer to the One-hot Encoding module of the Convert Categorical Variables into Quantitative Variables course.
Encoding categorical variables is important because it allows us to convert these variables into a format that can be understood by machine learning algorithms. In this step, we will practice encoding nominal and ordinal data using an ordinal encoding and one-hot encoding.
In this hands-on practice, we will be encoding nominal and ordinal data using Pandas's
Series.replace()
method. We'll also use one-hot encoding. We will use Pandas'sPandas.get_dummies()
function to perform one-hot encoding.
Task 1.1: Load the Dataset and Visualize
Load the dataset 'Qualitative_Data.csv' into a pandas DataFrame and display the first 5 rows of the data.
π Hint
Use the
pd.read_csv()
function to load the dataset, and theDataFrame.head()
method to display the first 5 rows. The file path is 'Qualitative_Data.csv'.π Solution
import pandas as pd df = pd.read_csv('Qualitative_Data.csv') print(df.head())
Task 1.2: Ordinal Encoding without Overwriting
Encode the 'Product Feedback' column using ordinal encoding and store the encoded values in a new column named 'Product Feedback Encoded'. The categories in the column are 'Positive', 'Neutral', and 'Negative'. Encode these as 2, 1, and 0 respectively.
π Hint
Create a dictionary with the following mapping:
{'Positive': 2, 'Neutral': 1, 'Negative': 0}
. Use this dictionary with theSeries.replace()
method to create the new column.π Solution
feedback_dict = {'Positive': 2, 'Neutral': 1, 'Negative': 0} df['Product Feedback Encoded'] = df['Product Feedback'].replace(feedback_dict)
Task 1.3: In-Place One-hot Encoding with Prefix
Perform one-hot encoding on the 'Social Media Activity' column in the
df
DataFrame itself, adding a prefix to the new columns to identify them. Use thepd.get_dummies()
function with an in-place update.π Hint
Use the
pd.get_dummies()
function with thecolumns=['Social Media Activity']
andprefix='SMA'
arguments. Thepd.get_dummies()
function will return the DataFrame with the one-hot encoded columns, which should then replace the originaldf
.π Solution
df = pd.get_dummies(df, columns=['Social Media Activity'], prefix='SMA')
Task 1.4: Inspect the Updated DataFrame
Print the first 5 rows of the updated DataFrame
df
with the one-hot encoded columns.π Hint
Use the
DataFrame.head()
method to print the first 5 rows of the updated DataFramedf
.π Solution
print(df.head())
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the authorβs guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.