A categorical data type in pandas is a way to store data that has a limited set of possible values, called categories. It saves memory by storing the data as integers internally and mapping them to the categories.
Click to reveal answer
beginner
How does using categorical data types save memory compared to using strings?
Using categorical data types saves memory because it stores each unique value only once and uses integer codes to represent the data, instead of storing full strings repeatedly.
Click to reveal answer
beginner
Which pandas method converts a column to a categorical type?
The pandas method to convert a column to categorical type is astype('category').
Click to reveal answer
beginner
What is one common use case for converting data to categorical type?
A common use case is when a column has repeated values from a small set, like days of the week or product categories, to reduce memory and speed up operations.
Click to reveal answer
beginner
True or False: Categorical data types can only be used with text data.
False. Categorical data types can be used with any data that has a limited set of values, including numbers and strings.
Click to reveal answer
What does pandas store internally when using a categorical data type?
AFloating point numbers
BInteger codes representing categories
CFull string values for each row
DBoolean values
✗ Incorrect
Pandas stores integer codes internally for categorical data, mapping them to the category values.
Which pandas function converts a column to categorical?
Aastype('category')
Bto_categorical()
Cconvert_category()
Dcategory_convert()
✗ Incorrect
The correct method is astype('category') to convert a column to categorical type.
Why do categorical types save memory?
AThey remove missing values
BThey compress data using zip
CThey store unique values once and use codes
DThey convert data to integers only
✗ Incorrect
Categorical types save memory by storing unique values once and using integer codes for data.
Which data is best suited for categorical type?
AUnique IDs with many different values
BContinuous numerical data
CRandom floating point numbers
DRepeated values from a small set
✗ Incorrect
Categorical types are best for repeated values from a small set, like categories.
Can categorical types be used for numeric data?
AYes, if the numbers represent categories
BNo, only for text data
COnly for floating point numbers
DOnly for boolean data
✗ Incorrect
Categorical types can be used for numeric data if the numbers represent categories.
Explain how converting a pandas column to categorical type helps save memory.
Think about how repeated values are stored in normal strings versus categorical.
You got /4 concepts.
Describe a real-life example where using categorical data type would be beneficial.
Consider data with many repeated labels.
You got /4 concepts.
Practice
(1/5)
1.
What is the main benefit of converting a pandas column to category type?
easy
A. It sorts the data automatically
B. It saves memory by storing repeated values efficiently
C. It converts numbers to strings
D. It removes missing values
Solution
Step 1: Understand what category type does
The category type stores repeated text values once and uses codes internally.
Step 2: Identify the main benefit
This reduces memory usage because repeated strings are not stored multiple times.
Final Answer:
It saves memory by storing repeated values efficiently -> Option B
Quick Check:
Memory savings = A [OK]
Hint: Remember: categories store repeated text once [OK]
Common Mistakes:
Thinking it sorts data automatically
Confusing category with string conversion
Assuming it removes missing values
2.
Which of the following is the correct way to convert a pandas DataFrame column df['color'] to a categorical type?
The argument to astype must be a string 'category', but here category is unquoted, causing a NameError.
Step 2: Verify other parts
DataFrame creation and column name are correct; astype supports 'category'.
Final Answer:
Missing quotes around 'category' in astype -> Option C
Quick Check:
Quotes needed for 'category' string [OK]
Hint: Always quote 'category' in astype('category') [OK]
Common Mistakes:
Forgetting quotes around 'category'
Thinking astype can't convert to category
Assuming column name is wrong
5.
You have a DataFrame with a column df['status'] containing 1 million rows with only 3 unique values: 'new', 'in_progress', 'done'. How can you reduce memory usage effectively?
hard
A. Replace values with integers manually without category
B. Convert df['status'] to string type
C. Drop the df['status'] column
D. Convert df['status'] to category type
Solution
Step 1: Understand the data characteristics
With only 3 unique values repeated 1 million times, storing as category saves memory by storing unique values once.
Step 2: Compare options
Converting to string keeps large memory; dropping column loses data; manual integer replacement is error-prone and less efficient.
Final Answer:
Convert df['status'] to category type -> Option D
Quick Check:
Few unique values + large rows = category [OK]
Hint: Use category for few unique values in large data [OK]
Common Mistakes:
Using string type which uses more memory
Dropping column losing important data
Manual integer replacement without category benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.