Recall & Review
beginner
What is a categorical variable in pandas?
A categorical variable is a type of variable that can take on a limited, fixed number of possible values, called categories or labels. In pandas, it is stored using the 'category' data type to save memory and improve performance.
Click to reveal answer
beginner
How do you convert a pandas column to a categorical type?
Use the method
pd.Series.astype('category') to convert a column to categorical type. For example: df['col'] = df['col'].astype('category').Click to reveal answer
intermediate
What are category codes in pandas?
Category codes are integer values that represent the position of each category label in the list of categories. They are stored internally to make operations faster and use less memory.
Click to reveal answer
intermediate
How can you access the category codes of a pandas categorical column?
You can access the codes using the
.cat.codes attribute. For example: df['col'].cat.codes returns the integer codes for each value in the column.Click to reveal answer
intermediate
How do you get the list of category labels in a pandas categorical column?
Use the
.cat.categories attribute to get the list of category labels. For example: df['col'].cat.categories returns the categories as an Index object.Click to reveal answer
What does the
.cat.codes attribute return for a pandas categorical column?✗ Incorrect
The .cat.codes attribute returns integer codes that represent the position of each category label in the categories list.
How do you convert a pandas column named 'color' to categorical type?
✗ Incorrect
Using astype('category') converts the column to categorical type.
Which attribute gives you the list of category labels in a pandas categorical column?
✗ Incorrect
The .cat.categories attribute returns the list of category labels.
Why use categorical data type in pandas?
✗ Incorrect
Categorical data type saves memory and speeds up operations by storing categories as codes.
If a category label is missing in the categories list, what will its code be?
✗ Incorrect
Missing or unknown categories have a code of -1 in pandas.
Explain what category codes and category labels are in pandas and how they relate to each other.
Think about how pandas stores categories internally.
You got /4 concepts.
Describe how to convert a pandas DataFrame column to categorical type and how to access its codes and labels.
Remember the .cat accessor for categorical columns.
You got /3 concepts.