0
0
Pandasdata~10 mins

Why categorical type matters in Pandas - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 'color' column to categorical type.

Pandas
df['color'] = df['color'].[1]()
Drag options to blanks, or click blank then click option'
Aastype('category')
Bto_numeric
Cstr
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_numeric tries to convert to numbers, which is wrong here.
Using str converts to string, not categorical.
2fill in blank
medium

Complete the code to check the memory usage of the DataFrame.

Pandas
memory_before = df.memory_[1](deep=True)
Drag options to blanks, or click blank then click option'
Ausage
Bsize
Cinfo
Ddescribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using size returns number of elements, not memory.
Using info prints info but does not return memory usage.
3fill in blank
hard

Fix the error in the code to convert the 'size' column to categorical.

Pandas
df['size'] = df['size'].astype([1])
Drag options to blanks, or click blank then click option'
Astr
B'category'
C'categorical'
Dcategory
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the type name without quotes causes an error.
Using 'categorical' is not a valid dtype.
4fill in blank
hard

Fill both blanks to create a dictionary of counts for categories with more than 2 entries.

Pandas
counts = {cat: count for cat, count in df['fruit'].value_counts().items() if count [1] [2]
Drag options to blanks, or click blank then click option'
A>
B2
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' will filter wrong categories.
Forgetting to compare count to a number.
5fill in blank
hard

Fill all three blanks to create a new categorical column with ordered categories.

Pandas
df['rating_cat'] = pd.Categorical(df['rating'], categories=[1], ordered=[2])

unique_cats = df['rating_cat'].[3]()
Drag options to blanks, or click blank then click option'
A['low', 'medium', 'high']
BTrue
Cunique
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting ordered to False when order matters.
Using unique without parentheses.