Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Data
Labs

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.

Lab platform
Lab Info
Level
Beginner
Last updated
Oct 02, 2025
Duration
12m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. 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's Pandas.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 the DataFrame.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 the Series.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 the pd.get_dummies() function with an in-place update.

    🔍 Hint

    Use the pd.get_dummies() function with the columns=['Social Media Activity'] and prefix='SMA' arguments. The pd.get_dummies() function will return the DataFrame with the one-hot encoded columns, which should then replace the original df.

    🔑 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 DataFrame df.

    🔑 Solution
    print(df.head())
    
About the author

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight