A categorical data type is a way to store data that has a limited set of possible values, like colors or categories, which helps save memory and speeds up analysis.
Click to reveal answer
beginner
Why should you convert string columns with repeated values to categorical type?
Because categorical type stores each unique value only once and uses codes for the data, reducing memory use and making operations faster.
Click to reveal answer
intermediate
How does pandas represent categorical data internally?
Pandas stores categorical data as integer codes that point to a list of unique categories, which saves space compared to storing full strings repeatedly.
Click to reveal answer
beginner
What is a common method to convert a column to categorical in pandas?
Use the pandas function: df['column'] = df['column'].astype('category') to convert the column to categorical type.
Click to reveal answer
intermediate
What is a potential downside of using categorical data type?
If the column has many unique values (high cardinality), converting to categorical might not save memory and can add overhead.
Click to reveal answer
What does converting a column to categorical type mainly help with?
AIncreasing the number of unique values
BReducing memory usage
CChanging data to numbers only
DRemoving missing values
✗ Incorrect
Categorical type reduces memory by storing unique values once and using codes.
Which pandas method converts a column to categorical?
Adf['col'].astype('category')
Bdf['col'].to_numeric()
Cdf['col'].fillna()
Ddf['col'].unique()
✗ Incorrect
astype('category') changes the column type to categorical.
What is stored internally for categorical data in pandas?
ABoolean values
BFull strings repeated
CInteger codes and categories list
DOnly numbers
✗ Incorrect
Pandas stores integer codes pointing to unique categories.
When might categorical type NOT save memory?
AWhen there are many unique values
BWhen data is numeric
CWhen data has missing values
DWhen data is already numeric
✗ Incorrect
High cardinality means many unique values, reducing memory savings.
Which of these is a benefit of categorical data type?
AConverting numbers to strings
BMore precise floating point calculations
CAutomatic data cleaning
DFaster comparisons and grouping
✗ Incorrect
Categorical data speeds up operations like grouping and comparisons.
Explain what categorical data type is and why it is useful in data analysis.
Think about how repeated strings can be stored more efficiently.
You got /4 concepts.
Describe how to convert a string column to categorical in pandas and when you should do it.
Focus on the pandas method and the type of data that benefits most.
You got /4 concepts.
Practice
(1/5)
1.
What is the main benefit of converting a DataFrame column to category type in pandas?
easy
A. It reduces memory usage by storing unique values once
B. It automatically sorts the column values alphabetically
C. It converts numeric values to strings
D. It removes missing values from the column
Solution
Step 1: Understand what categorical type does
The categorical type stores unique values only once and uses codes internally, saving memory.
Step 2: Compare with other options
Sorting, converting to strings, or removing missing values are not automatic effects of categorical conversion.
Final Answer:
It reduces memory usage by storing unique values once -> Option A
Quick Check:
Categorical type = Memory saving [OK]
Hint: Categorical stores unique values once to save memory [OK]
Common Mistakes:
Thinking it sorts values automatically
Confusing with string conversion
Assuming it removes missing data
2.
Which of the following is the correct syntax to convert a pandas DataFrame column df['color'] to categorical type?
The argument to astype() must be a string 'category', not a bare word.
Step 2: Verify other parts of code
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 dtype string [OK]
Hint: Always put quotes around dtype names in astype() [OK]
Common Mistakes:
Forgetting quotes around 'category'
Assuming astype() can't convert to category
Mistaking column name errors
5.
You have a DataFrame with a column 'city' containing 1 million rows but only 5 unique city names. Which approach optimizes memory and speeds up grouping by city?
hard
A. Use df.groupby('city') without any conversion
B. Convert 'city' column to string type before grouping
C. Drop duplicates from 'city' column before grouping
D. Convert 'city' column to categorical type before grouping
Solution
Step 1: Understand memory and speed benefits of categorical
Converting to categorical stores unique values once, reducing memory and speeding grouping.
Step 2: Evaluate other options
String type uses more memory; dropping duplicates loses data; grouping without conversion is slower.
Final Answer:
Convert 'city' column to categorical type before grouping -> Option D
Quick Check:
Categorical = Memory + speed optimization [OK]
Hint: Convert to categorical before grouping large repeated data [OK]
Common Mistakes:
Using string type wastes memory
Dropping duplicates loses data
Grouping without conversion is slower
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.