📖 Scenario: Imagine you work in a store that sells different types of fruits. You have a list of fruits sold each day, but some fruits appear many times. You want to organize this data efficiently and understand how many times each fruit was sold.
🎯 Goal: You will create a list of fruits sold, convert it to a categorical type in pandas, and then compare the memory usage and counts of each fruit. This will show why using categorical data is helpful.
📋 What You'll Learn
Create a pandas Series with fruit names
Create a categorical version of the Series
Compare memory usage of both Series
Count how many times each fruit appears
💡 Why This Matters
🌍 Real World
Stores, surveys, and many datasets have repeated categories like product types or answers. Using categorical types helps save memory and speeds up analysis.
💼 Career
Data analysts and scientists often work with large datasets. Knowing how to use categorical data types helps optimize performance and resource use.
Progress0 / 4 steps
1
Create a pandas Series with fruit names
Import pandas as pd and create a pandas Series called fruits with these exact values: ["apple", "banana", "apple", "orange", "banana", "apple"].
Pandas
Hint
Use pd.Series() to create the Series from the list of fruits.
2
Create a categorical version of the Series
Create a new variable called fruits_cat by converting the fruits Series to categorical type using pd.Series.astype('category').
Pandas
Hint
Use astype('category') on the fruits Series.
3
Compare memory usage of both Series
Create two variables: mem_fruits and mem_fruits_cat. Use the memory_usage(deep=True) method on fruits and fruits_cat respectively to get their memory usage.
Pandas
Hint
Use memory_usage(deep=True) on both Series and save the results.
4
Print memory usage and counts of each fruit
Print the memory usage variables mem_fruits and mem_fruits_cat on separate lines. Then print the counts of each fruit in fruits_cat using the value_counts() method.
Pandas
Hint
Use print() to show memory usage and value_counts() to count fruits.
Practice
(1/5)
1. Why is using the categorical data type in pandas helpful when you have a column with many repeated text values?
easy
A. It converts text to numbers for calculations.
B. It automatically sorts the data alphabetically.
C. It saves memory by storing repeated values efficiently.
D. It removes duplicate rows from the DataFrame.
Solution
Step 1: Understand memory usage of text data
Repeated text values take more memory because each string is stored separately.
Step 2: How categorical type optimizes memory
Categorical type stores unique values once and uses codes to represent them, saving memory.
Final Answer:
It saves memory by storing repeated values efficiently. -> Option C
Expecting set_categories to reorder data automatically
Thinking sort_values fails on categorical columns
Trying to convert to string before sorting
5. You have a large DataFrame with a column 'city' containing many repeated city names. You want to reduce memory usage and speed up filtering for cities in a specific order: 'New York', 'Los Angeles', 'Chicago'. Which approach is best?
hard
A. Keep 'city' as string type and sort the DataFrame by 'city' alphabetically
B. Convert 'city' to categorical with ordered categories: ['New York', 'Los Angeles', 'Chicago']
C. Convert 'city' to numeric codes without setting categories
D. Remove duplicate city names from the DataFrame
Solution
Step 1: Why use categorical with order
Ordered categorical saves memory and allows meaningful sorting/filtering by specified city order.
Step 2: Why other options are less suitable
Keeping strings uses more memory; numeric codes without categories lose order meaning; removing duplicates loses data.
Final Answer:
Convert 'city' to categorical with ordered categories: ['New York', 'Los Angeles', 'Chicago'] -> Option B
Quick Check:
Ordered categorical = A [OK]
Hint: Use ordered categorical for memory and custom order [OK]
Common Mistakes:
Not setting categories order
Using numeric codes without categories
Removing duplicates instead of optimizing type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.