Bird
Raised Fist0
Pandasdata~10 mins

Category codes and labels in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Category codes and labels
Create categorical data
↓
Assign categories with labels
↓
Access category codes
↓
Use codes for analysis or storage
↓
Map codes back to labels if needed
This flow shows how to create categorical data, assign labels, access their integer codes, and use them for efficient analysis.
Execution Sample
Pandas
import pandas as pd
colors = pd.Series(['red', 'blue', 'green', 'blue', 'red'])
cats = colors.astype('category')
codes = cats.cat.codes
labels = cats.cat.categories
This code creates a categorical series from color names, then extracts integer codes and category labels.
Execution Table
StepActionData/VariableResult/Value
1Create Series with colorscolors0:red,1:blue,2:green,3:blue,4:red
2Convert to categoricalcatsCategories: ['blue', 'green', 'red'] with codes [2,0,1,0,2]
3Access category codescodes[2,0,1,0,2]
4Access category labelslabelsIndex(['blue', 'green', 'red'])
5Use codes for indexing or storagecodesInteger array representing categories
6Map codes back to labelscodes + labels2->red,0->blue,1->green
7EndAll categories and codes assigned correctly
💡 All categorical codes and labels extracted and mapped successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
colorsEmpty['red','blue','green','blue','red']SameSameSame
catsN/ACategorical with categories ['blue','green','red']SameSameSame
codesN/AN/A[2,0,1,0,2]SameSame
labelsN/AN/AN/A['blue','green','red']Same
Key Moments - 2 Insights
Why do the category codes not match the original order of colors?
Category codes are assigned based on sorted unique categories (alphabetical order), not the original order. See execution_table step 2 where categories are ['blue', 'green', 'red'] sorted alphabetically.
How can I get the original color names back from the codes?
Use the category labels with the codes to map back. For example, code 0 corresponds to 'blue' as shown in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the code for the color 'green'?
A1
B2
C0
D3
💡 Hint
Check the codes array in step 3 and the category order in step 2.
At which step do we convert the original Series into categorical data?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the action 'Convert to categorical' in the execution_table.
If a new color 'yellow' is added, what happens to the category codes?
AAll codes shift by one
BCodes remain the same for existing colors, 'yellow' gets a new code
CCodes become strings instead of integers
DCodes reset to zero for all colors
💡 Hint
Think about how pandas assigns codes only to new categories without changing existing ones.
Concept Snapshot
Category codes and labels in pandas:
- Convert a Series to 'category' dtype.
- Categories are sorted unique values.
- Codes are integer indexes for categories.
- Use .cat.codes for codes, .cat.categories for labels.
- Codes help efficient storage and analysis.
Full Transcript
We start by creating a pandas Series with color names. Then, we convert this Series to a categorical type, which organizes unique colors as categories sorted alphabetically. Each category gets an integer code starting from zero. We extract these codes using .cat.codes and the category labels using .cat.categories. These codes represent the original data efficiently and can be mapped back to the labels when needed. This process helps in saving memory and speeding up analysis when working with repeated categorical data.

Practice

(1/5)
1. What does cat.codes show in a pandas categorical Series?
easy
A. The data type of the Series
B. The numeric codes representing each category
C. The count of each category
D. The text labels of the categories

Solution

  1. Step 1: Understand what cat.codes returns

    cat.codes gives the integer codes for each category in the Series, replacing repeated text with numbers.
  2. Step 2: Differentiate from cat.categories

    cat.categories shows the text labels, not the codes.
  3. Final Answer:

    The numeric codes representing each category -> Option B
  4. Quick Check:

    cat.codes = numeric codes [OK]
Hint: Remember: codes are numbers, categories are labels [OK]
Common Mistakes:
  • Confusing codes with category labels
  • Thinking codes show counts
  • Assuming codes show data type
2. Which of the following is the correct way to access the category labels of a pandas categorical Series s?
easy
A. s.cat.categories
B. s.cat.codes
C. s.categories
D. s.cat.labels

Solution

  1. Step 1: Recall the attribute for category labels

    The category labels are accessed via cat.categories on the Series.
  2. Step 2: Check the options for correct syntax

    s.cat.categories is the correct syntax; s.categories is invalid, and cat.labels does not exist.
  3. Final Answer:

    s.cat.categories -> Option A
  4. Quick Check:

    Category labels = s.cat.categories [OK]
Hint: Use cat.categories to get labels, not just categories [OK]
Common Mistakes:
  • Using s.categories instead of s.cat.categories
  • Confusing cat.codes with labels
  • Trying to use non-existent cat.labels
3. Given the code:
import pandas as pd
s = pd.Series(['apple', 'banana', 'apple', 'orange'], dtype='category')
print(s.cat.codes.tolist())

What is the output?
medium
A. [0, 1, 0, 2]
B. [1, 2, 1, 3]
C. [0, 0, 1, 2]
D. [1, 0, 1, 2]

Solution

  1. Step 1: Identify categories and their order

    Categories are sorted alphabetically by default: ['apple', 'banana', 'orange'] with codes 0,1,2 respectively.
  2. Step 2: Map each value to its code

    'apple' -> 0, 'banana' -> 1, 'apple' -> 0, 'orange' -> 2, so the list is [0, 1, 0, 2].
  3. Final Answer:

    [0, 1, 0, 2] -> Option A
  4. Quick Check:

    Alphabetical categories -> codes match [OK]
Hint: Categories sorted alphabetically by default [OK]
Common Mistakes:
  • Assuming codes start at 1 instead of 0
  • Ignoring alphabetical sorting of categories
  • Mixing up category order
4. Which of the following is correct about this code snippet?
import pandas as pd
s = pd.Series(['cat', 'dog', 'cat'], dtype='category')
s.cat.categories = ['dog', 'cat']
print(s.cat.codes.tolist())
medium
A. You cannot assign new categories with different order directly
B. The categories list must be sorted alphabetically
C. You must convert Series to string before assigning categories
D. The code will run without errors and print [0, 1, 0]

Solution

  1. Step 1: Understand category assignment rules

    Assigning to cat.categories redefines the labels for existing codes without changing the codes or raising errors, as long as new list has same length and unique values.
  2. Step 2: Trace execution

    Original categories ['cat', 'dog'], codes [0,1,0]. New ['dog', 'cat'] same length/unique, codes unchanged, prints [0, 1, 0].
  3. Final Answer:

    The code will run without errors and print [0, 1, 0] -> Option D
  4. Quick Check:

    New categories redefine code labels, codes unchanged [OK]
Hint: Reassigning cat.categories changes labels but keeps codes intact [OK]
Common Mistakes:
  • Thinking reordering causes ValueError
  • Expecting codes to adjust to new order like [1, 0, 1]
  • Assuming categories must stay alphabetical
5. You have a pandas categorical Series s with categories ['low', 'medium', 'high']. You want to rename the categories to ['L', 'M', 'H'] without changing the data. Which code correctly returns the renamed Series?
hard
A. s.cat.categories = ['L', 'M', 'H']
B. s.cat.categories.rename(['L', 'M', 'H'])
C. s.cat.rename_categories(['L', 'M', 'H'])
D. s.rename_categories(['L', 'M', 'H'])

Solution

  1. Step 1: Understand how to rename categories

    The method cat.rename_categories() returns a new Series with renamed categories without changing data codes.
  2. Step 2: Check other options for correctness

    Direct assignment to cat.categories works but can cause errors if not careful; cat.categories.rename() is invalid; rename_categories() is not a Series method.
  3. Final Answer:

    s.cat.rename_categories(['L', 'M', 'H']) -> Option C
  4. Quick Check:

    Use cat.rename_categories() to rename safely [OK]
Hint: Use cat.rename_categories() to rename categories safely [OK]
Common Mistakes:
  • Trying to rename categories by invalid methods
  • Directly assigning categories without matching length
  • Confusing Series methods with cat accessor methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard