0
0
Pandasdata~15 mins

Converting to categorical in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the DataFrame
Create a pandas DataFrame called df with one column named satisfaction containing these exact values in order: 'Low', 'Medium', 'High', 'Medium', 'Low'.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where the key is 'satisfaction' and the value is the list of satisfaction levels.

2
Define the categories
Create a variable called categories that is a list containing the strings 'Low', 'Medium', and 'High' in this exact order.
Pandas
Need a hint?

Just assign the list ['Low', 'Medium', 'High'] to the variable categories.

3
Convert the column to categorical
Convert the satisfaction column in df to a categorical type using the categories list. Use pd.Categorical with the categories argument and set ordered=True. Assign the result back to df['satisfaction'].
Pandas
Need a hint?

Use pd.Categorical to convert the column and assign it back to df['satisfaction'].

4
Print the data type
Print the data type of the satisfaction column in df using print(df['satisfaction'].dtype).
Pandas
Need a hint?

Use print(df['satisfaction'].dtype) to see the data type after conversion.