0
0
Pandasdata~10 mins

Ordered categories 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 create an ordered categorical type with levels 'low', 'medium', 'high'.

Pandas
import pandas as pd
levels = ['low', 'medium', 'high']
cat_type = pd.CategoricalDtype(categories=levels, ordered=[1])
Drag options to blanks, or click blank then click option'
ATrue
B0
CNone
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting ordered=False when order matters
Using None instead of True for ordered
2fill in blank
medium

Complete the code to convert a column 'priority' in DataFrame df to the ordered categorical type cat_type.

Pandas
df['priority'] = df['priority'].astype([1])
Drag options to blanks, or click blank then click option'
Alist
Bstr
Cint
Dcat_type
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'str' instead of the categorical type
Using 'int' or 'list' which are not categorical types
3fill in blank
hard

Fix the error in the code to compare if 'priority' is greater than 'medium'.

Pandas
result = df['priority'] [1] 'medium'
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality instead of comparison
Using less than or equal which checks the wrong condition
4fill in blank
hard

Fill both blanks to create a dictionary of counts for categories greater than 'low'.

Pandas
counts = {cat: sum(df['priority'] [1] cat) for cat in cat_type.categories if cat [2] 'low'}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality or not equal operators instead of greater than
Mixing different comparison operators in the two blanks
5fill in blank
hard

Fill both blanks to create a filtered DataFrame with priority between 'medium' and 'high' inclusive.

Pandas
filtered = df[(df['priority'] [1]= 'medium') & (df['priority'] [2]= 'high')]
Drag options to blanks, or click blank then click option'
A>=
B<=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators that exclude boundaries
Using != which excludes values