What does converting a column to categorical type in pandas do?
It changes the column to a special type that uses less memory and can speed up operations by storing data as categories instead of raw values.
Click to reveal answer
beginner
How do you convert a pandas DataFrame column named 'color' to categorical?
Use df['color'] = df['color'].astype('category') to convert the 'color' column to categorical type.
Click to reveal answer
beginner
Why might you want to convert a text column with repeated values to categorical?
Because it saves memory and can make filtering and grouping faster, just like organizing similar items into labeled boxes instead of keeping them loose.
Click to reveal answer
intermediate
What is the difference between ordered and unordered categorical data?
Ordered categorical data has a meaningful order (like small < medium < large), while unordered categorical data has no order (like red, blue, green).
Click to reveal answer
intermediate
How can you create an ordered categorical column in pandas?
Use df['size'] = pd.Categorical(df['size'], categories=['small', 'medium', 'large'], ordered=True) to create an ordered categorical column.
Click to reveal answer
What is the main benefit of converting a column to categorical in pandas?
ASaves memory and speeds up operations
BChanges numbers to strings
CDeletes duplicate rows
DSorts the data automatically
✗ Incorrect
Converting to categorical saves memory and can speed up operations by storing repeated values efficiently.
Which pandas method converts a column to categorical type?
Adf['col'].fillna()
Bdf['col'].to_string()
Cdf['col'].sort_values()
Ddf['col'].astype('category')
✗ Incorrect
The astype('category') method converts a column to categorical type.
What does ordered=True do when creating a categorical column?
ASorts the DataFrame
BDefines a meaningful order for categories
CConverts numbers to strings
DRemoves duplicates
✗ Incorrect
Setting ordered=True defines a meaningful order among categories.
Which of these is NOT a reason to use categorical data?
AAutomatically fix missing data
BReduce memory usage
CSpeed up filtering and grouping
DRepresent repeated text values efficiently
✗ Incorrect
Categorical data does not automatically fix missing data.
If you have a column with values 'red', 'blue', 'green', what type is best to convert it to?
AFloat
BInteger
CCategorical
DBoolean
✗ Incorrect
Categorical type is best for repeated text values like colors.
Explain why and how you would convert a text column with repeated values to categorical in pandas.
Think about how repeated labels can be stored more efficiently.
You got /4 concepts.
Describe the difference between ordered and unordered categorical data and give an example of each.
Consider sizes for ordered and colors for unordered.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of converting a text column to a categorical type in pandas?
easy
A. It removes duplicate rows from the DataFrame.
B. It changes the text to numbers automatically.
C. It saves memory and speeds up analysis.
D. It sorts the DataFrame by that column.
Solution
Step 1: Understand what categorical type does
Converting text to categorical stores repeated values efficiently, saving memory.
Step 2: Recognize impact on analysis speed
Operations on categorical data are faster because pandas uses codes internally.
Final Answer:
It saves memory and speeds up analysis. -> Option C
Quick Check:
Converting to categorical = Save memory and speed [OK]
Hint: Categorical = less memory + faster analysis [OK]
Common Mistakes:
Thinking it changes text to numbers visibly
Assuming it sorts data automatically
Confusing with removing duplicates
2. Which of the following is the correct syntax to convert a pandas DataFrame column df['color'] to categorical?
easy
A. df['color'] = df['color'].astype('category')
B. df['color'] = pd.to_categorical(df['color'])
C. df['color'] = df['color'].convert('category')
D. df['color'] = df['color'].category()
Solution
Step 1: Recall pandas method for type conversion
The correct method is astype('category') to convert column type.
Step 2: Check each option
df['color'] = df['color'].astype('category') uses astype('category'), which is correct. Others are invalid pandas syntax.
Final Answer:
df['color'] = df['color'].astype('category') -> Option A
Quick Check:
Use astype('category') to convert [OK]
Hint: Use astype('category') to convert columns [OK]
Common Mistakes:
Using non-existent methods like convert() or category()
Trying pd.to_categorical which is not a pandas function
B. astype() does not accept ordered=True argument.
C. pd.Categorical requires categories to be sorted alphabetically.
D. Missing import statement for pandas.
Solution
Step 1: Check astype() method parameters
The astype() method does not accept an ordered parameter.
Step 2: Identify correct way to set ordered
Ordering must be set when creating the categorical with pd.Categorical(..., ordered=True), not via astype.
Final Answer:
astype() does not accept ordered=True argument. -> Option B
Quick Check:
ordered=True only in pd.Categorical, not astype [OK]
Hint: ordered=True only works in pd.Categorical, not astype [OK]
Common Mistakes:
Trying to set ordered in astype()
Assuming categories must be sorted alphabetically
Thinking multiple conversions cause error
5. You have a DataFrame df with a column 'status' containing repeated text values: 'low', 'medium', 'high'. You want to convert it to a categorical type with the order low < medium < high, and then get the counts of each category in order. Which code snippet correctly does this?
hard
A. df['status'] = df['status'].astype('category', ordered=True)
print(df['status'].value_counts(sort=False))
B. df['status'] = df['status'].astype('category')
print(df['status'].value_counts())
C. df['status'] = pd.Categorical(df['status'])
print(df['status'].value_counts(sort=True))
D. df['status'] = pd.Categorical(df['status'], categories=['low', 'medium', 'high'], ordered=True)
print(df['status'].value_counts(sort=False))
Solution
Step 1: Convert with ordered categories
Use pd.Categorical with categories=['low', 'medium', 'high'] and ordered=True to set order.
Step 2: Get counts without sorting
Use value_counts(sort=False) to get counts in category order.