A categorical variable is a type of variable that can take on a limited, fixed number of possible values, called categories or labels. In pandas, it is stored using the 'category' data type to save memory and improve performance.
Click to reveal answer
beginner
How do you convert a pandas column to a categorical type?
Use the method pd.Series.astype('category') to convert a column to categorical type. For example: df['col'] = df['col'].astype('category').
Click to reveal answer
intermediate
What are category codes in pandas?
Category codes are integer values that represent the position of each category label in the list of categories. They are stored internally to make operations faster and use less memory.
Click to reveal answer
intermediate
How can you access the category codes of a pandas categorical column?
You can access the codes using the .cat.codes attribute. For example: df['col'].cat.codes returns the integer codes for each value in the column.
Click to reveal answer
intermediate
How do you get the list of category labels in a pandas categorical column?
Use the .cat.categories attribute to get the list of category labels. For example: df['col'].cat.categories returns the categories as an Index object.
Click to reveal answer
What does the .cat.codes attribute return for a pandas categorical column?
AThe data type of the column
BThe original string labels
CInteger codes representing category positions
DThe number of unique categories
✗ Incorrect
The .cat.codes attribute returns integer codes that represent the position of each category label in the categories list.
How do you convert a pandas column named 'color' to categorical type?
Adf['color'] = df['color'].astype('category')
Bdf['color'] = pd.to_numeric(df['color'])
Cdf['color'] = df['color'].astype('int')
Ddf['color'] = df['color'].astype('string')
✗ Incorrect
Using astype('category') converts the column to categorical type.
Which attribute gives you the list of category labels in a pandas categorical column?
A.cat.categories
B.cat.codes
C.dtype
D.unique()
✗ Incorrect
The .cat.categories attribute returns the list of category labels.
Why use categorical data type in pandas?
ATo convert numbers to strings
BTo remove missing values
CTo sort data alphabetically
DTo save memory and speed up operations
✗ Incorrect
Categorical data type saves memory and speeds up operations by storing categories as codes.
If a category label is missing in the categories list, what will its code be?
ANaN
B-1
C1
D0
✗ Incorrect
Missing or unknown categories have a code of -1 in pandas.
Explain what category codes and category labels are in pandas and how they relate to each other.
Think about how pandas stores categories internally.
You got /4 concepts.
Describe how to convert a pandas DataFrame column to categorical type and how to access its codes and labels.
Remember the .cat accessor for categorical columns.
You got /3 concepts.
Practice
(1/5)
1. What does cat.codes show in a pandas categorical Series?
easy
A. The data type of the Series
B. The numeric codes representing each category
C. The count of each category
D. The text labels of the categories
Solution
Step 1: Understand what cat.codes returns
cat.codes gives the integer codes for each category in the Series, replacing repeated text with numbers.
Step 2: Differentiate from cat.categories
cat.categories shows the text labels, not the codes.
Final Answer:
The numeric codes representing each category -> Option B
Quick Check:
cat.codes = numeric codes [OK]
Hint: Remember: codes are numbers, categories are labels [OK]
Common Mistakes:
Confusing codes with category labels
Thinking codes show counts
Assuming codes show data type
2. Which of the following is the correct way to access the category labels of a pandas categorical Series s?
easy
A. s.cat.categories
B. s.cat.codes
C. s.categories
D. s.cat.labels
Solution
Step 1: Recall the attribute for category labels
The category labels are accessed via cat.categories on the Series.
Step 2: Check the options for correct syntax
s.cat.categories is the correct syntax; s.categories is invalid, and cat.labels does not exist.
Final Answer:
s.cat.categories -> Option A
Quick Check:
Category labels = s.cat.categories [OK]
Hint: Use cat.categories to get labels, not just categories [OK]
Common Mistakes:
Using s.categories instead of s.cat.categories
Confusing cat.codes with labels
Trying to use non-existent cat.labels
3. Given the code:
import pandas as pd
s = pd.Series(['apple', 'banana', 'apple', 'orange'], dtype='category')
print(s.cat.codes.tolist())
What is the output?
medium
A. [0, 1, 0, 2]
B. [1, 2, 1, 3]
C. [0, 0, 1, 2]
D. [1, 0, 1, 2]
Solution
Step 1: Identify categories and their order
Categories are sorted alphabetically by default: ['apple', 'banana', 'orange'] with codes 0,1,2 respectively.
Step 2: Map each value to its code
'apple' -> 0, 'banana' -> 1, 'apple' -> 0, 'orange' -> 2, so the list is [0, 1, 0, 2].
Final Answer:
[0, 1, 0, 2] -> Option A
Quick Check:
Alphabetical categories -> codes match [OK]
Hint: Categories sorted alphabetically by default [OK]
Common Mistakes:
Assuming codes start at 1 instead of 0
Ignoring alphabetical sorting of categories
Mixing up category order
4. Which of the following is correct about this code snippet?
import pandas as pd
s = pd.Series(['cat', 'dog', 'cat'], dtype='category')
s.cat.categories = ['dog', 'cat']
print(s.cat.codes.tolist())
medium
A. You cannot assign new categories with different order directly
B. The categories list must be sorted alphabetically
C. You must convert Series to string before assigning categories
D. The code will run without errors and print [0, 1, 0]
Solution
Step 1: Understand category assignment rules
Assigning to cat.categories redefines the labels for existing codes without changing the codes or raising errors, as long as new list has same length and unique values.
Step 2: Trace execution
Original categories ['cat', 'dog'], codes [0,1,0]. New ['dog', 'cat'] same length/unique, codes unchanged, prints [0, 1, 0].
Final Answer:
The code will run without errors and print [0, 1, 0] -> Option D
Quick Check:
New categories redefine code labels, codes unchanged [OK]
Hint: Reassigning cat.categories changes labels but keeps codes intact [OK]
Common Mistakes:
Thinking reordering causes ValueError
Expecting codes to adjust to new order like [1, 0, 1]
Assuming categories must stay alphabetical
5. You have a pandas categorical Series s with categories ['low', 'medium', 'high']. You want to rename the categories to ['L', 'M', 'H'] without changing the data. Which code correctly returns the renamed Series?
hard
A. s.cat.categories = ['L', 'M', 'H']
B. s.cat.categories.rename(['L', 'M', 'H'])
C. s.cat.rename_categories(['L', 'M', 'H'])
D. s.rename_categories(['L', 'M', 'H'])
Solution
Step 1: Understand how to rename categories
The method cat.rename_categories() returns a new Series with renamed categories without changing data codes.
Step 2: Check other options for correctness
Direct assignment to cat.categories works but can cause errors if not careful; cat.categories.rename() is invalid; rename_categories() is not a Series method.
Final Answer:
s.cat.rename_categories(['L', 'M', 'H']) -> Option C
Quick Check:
Use cat.rename_categories() to rename safely [OK]
Hint: Use cat.rename_categories() to rename categories safely [OK]
Common Mistakes:
Trying to rename categories by invalid methods
Directly assigning categories without matching length
Confusing Series methods with cat accessor methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.