Converting to categorical
📖 Scenario: You work in a company that collects customer feedback. The feedback includes a column for customer satisfaction levels recorded as text labels like 'Low', 'Medium', and 'High'. You want to convert these text labels into a categorical type to save memory and prepare for analysis.
🎯 Goal: Convert a pandas DataFrame column with text labels into a categorical data type.
📋 What You'll Learn
Create a pandas DataFrame with a column named
satisfaction containing the exact values: 'Low', 'Medium', 'High', 'Medium', 'Low'.Create a variable called
categories that lists the categories in order: 'Low', 'Medium', 'High'.Convert the
satisfaction column to a categorical type using the categories variable.Print the
satisfaction column's data type after conversion.💡 Why This Matters
🌍 Real World
Categorical data types save memory and improve performance when working with columns that have repeated text values, like survey responses or product categories.
💼 Career
Data scientists and analysts often convert text columns to categorical types to optimize data storage and prepare data for machine learning models.
Progress0 / 4 steps