0
0
Pandasdata~10 mins

Category codes and labels 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 a categorical column from a list of colors.

Pandas
import pandas as pd
colors = ['red', 'blue', 'green', 'blue', 'red']
cat_colors = pd.Categorical([1])
print(cat_colors)
Drag options to blanks, or click blank then click option'
Apd.Series(colors)
B['red', 'blue']
C['green', 'blue']
Dcolors
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list with only some colors instead of the full list.
Passing a pandas Series instead of a list.
2fill in blank
medium

Complete the code to get the category codes from the categorical data.

Pandas
import pandas as pd
colors = pd.Categorical(['red', 'blue', 'green', 'blue', 'red'])
codes = colors.[1]
print(codes)
Drag options to blanks, or click blank then click option'
Alabels
Bvalues
Ccodes
Dcategories
Attempts:
3 left
💡 Hint
Common Mistakes
Using labels which is deprecated.
Using categories which returns category names, not codes.
3fill in blank
hard

Fix the error in the code to print the category labels of the categorical data.

Pandas
import pandas as pd
colors = pd.Categorical(['red', 'blue', 'green', 'blue', 'red'])
print(colors.[1])
Drag options to blanks, or click blank then click option'
Alabels
Bcategories
Ccodes
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using labels which is deprecated and causes error.
Using codes which returns integers, not labels.
4fill in blank
hard

Fill both blanks to create a categorical from a list and print its codes.

Pandas
import pandas as pd
fruits = ['apple', 'banana', 'apple', 'orange']
cat_fruits = pd.Categorical([1])
print(cat_fruits.[2])
Drag options to blanks, or click blank then click option'
Afruits
Bcodes
Ccategories
D['apple', 'banana']
Attempts:
3 left
💡 Hint
Common Mistakes
Using categories instead of codes for the second blank.
Passing a wrong list to pd.Categorical().
5fill in blank
hard

Fill all three blanks to create a categorical with custom categories and print codes and categories.

Pandas
import pandas as pd
animals = ['cat', 'dog', 'bird', 'dog', 'cat']
cat_animals = pd.Categorical([1], categories=[2], ordered=True)
print(cat_animals.[3])
print(cat_animals.categories)
Drag options to blanks, or click blank then click option'
Aanimals
B['dog', 'cat', 'bird']
Ccodes
D['cat', 'dog', 'bird']
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong order of categories.
Printing categories twice instead of codes and categories.