0
0
Pandasdata~10 mins

Converting to categorical in Pandas - Interactive Code Practice

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'
Atolist
Bto_numeric
Cstr
Dastype('category')
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_numeric instead of astype('category')
Trying to convert to string instead of categorical
2fill in blank
medium

Complete the code to get the categories of the 'color' column after conversion.

Pandas
categories = df['color'].[1]
Drag options to blanks, or click blank then click option'
Acat.categories
Bunique()
Cvalues
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using unique() which returns unique values but not categories
Using dtype which returns data type, not categories
3fill in blank
hard

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

Pandas
df['size'] = pd.Categorical(df['size'], categories=[1], ordered=True)
Drag options to blanks, or click blank then click option'
A['small', 'medium', 'large']
B['small', 'large', 'medium']
C['tiny', 'small', 'big']
D['large', 'medium', 'small']
Attempts:
3 left
💡 Hint
Common Mistakes
Using categories in wrong order
Using categories not matching the data
4fill in blank
hard

Fill both blanks to create a new categorical column 'rating_cat' from 'rating' with categories and order.

Pandas
df['rating_cat'] = pd.Categorical(df['rating'], categories=[1], ordered=[2])
Drag options to blanks, or click blank then click option'
A['low', 'medium', 'high']
BTrue
CFalse
D['high', 'medium', 'low']
Attempts:
3 left
💡 Hint
Common Mistakes
Setting ordered to False when order matters
Using categories in wrong order
5fill in blank
hard

Fill all three blanks to create a categorical column 'grade_cat' from 'grade' with categories, order, and rename categories.

Pandas
df['grade_cat'] = pd.Categorical(df['grade'], categories=[1], ordered=[2])
df['grade_cat'] = df['grade_cat'].cat.[3](['Poor', 'Average', 'Good'])
Drag options to blanks, or click blank then click option'
A['C', 'B', 'A']
BTrue
Crename_categories
Dset_categories
Attempts:
3 left
💡 Hint
Common Mistakes
Using rename_categories which is not a pandas method
Not setting ordered=True when order matters