Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new category 'D' to the categorical column.
Pandas
import pandas as pd cats = pd.Categorical(['A', 'B', 'A', 'C']) cats = cats.add_categories([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the category name.
Passing a list instead of a single string when adding one category.
✗ Incorrect
Use a string in quotes to add a new category 'D'.
2fill in blank
mediumComplete the code to remove the category 'B' from the categorical column.
Pandas
import pandas as pd cats = pd.Categorical(['A', 'B', 'A', 'C'], categories=['A', 'B', 'C']) cats = cats.remove_categories([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the category without quotes.
Passing a list instead of a single string.
✗ Incorrect
Remove category 'B' by passing it as a string in quotes.
3fill in blank
hardFix the error in the code to add multiple categories 'D' and 'E' to the categorical column.
Pandas
import pandas as pd cats = pd.Categorical(['A', 'B', 'A', 'C']) cats = cats.add_categories([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing categories as a single string with spaces.
Passing multiple strings without a list.
✗ Incorrect
To add multiple categories, pass a list of strings.
4fill in blank
hardFill both blanks to create a categorical column with categories 'A', 'B', 'C', then remove 'B'.
Pandas
import pandas as pd cats = pd.Categorical(['A', 'B', 'C'], categories=[1]) cats = cats.remove_categories([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing categories as a string instead of a list.
Removing category by passing a list instead of a string.
✗ Incorrect
Set categories as a list and remove category 'B' as a string.
5fill in blank
hardFill all three blanks to add categories 'D' and 'E', then remove 'A' from the categorical column.
Pandas
import pandas as pd cats = pd.Categorical(['A', 'B', 'C'], categories=[1]) cats = cats.add_categories([2]) cats = cats.remove_categories([3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing strings instead of lists for categories or added categories.
Removing category by passing a list instead of a string.
✗ Incorrect
Start with categories A, B, C; add D and E; then remove A.