0
0
Pandasdata~5 mins

Category codes and labels in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe data type of the column
BThe original string labels
CInteger codes representing category positions
DThe number of unique categories
How do you convert a pandas column named 'color' to categorical type?
Adf['color'] = df['color'].astype('category')
Bdf['color'] = pd.to_numeric(df['color'])
Cdf['color'] = df['color'].astype('int')
Ddf['color'] = df['color'].astype('string')
Which attribute gives you the list of category labels in a pandas categorical column?
A.cat.categories
B.cat.codes
C.dtype
D.unique()
Why use categorical data type in pandas?
ATo convert numbers to strings
BTo remove missing values
CTo sort data alphabetically
DTo save memory and speed up operations
If a category label is missing in the categories list, what will its code be?
ANaN
B-1
C1
D0
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.