Category codes and labels
📖 Scenario: You work in a small store that tracks product categories. Each product belongs to a category like 'Fruit', 'Vegetable', or 'Dairy'. You want to organize these categories using codes to make analysis easier.
🎯 Goal: Create a pandas DataFrame with product names and categories. Then convert the category column to a categorical type. Finally, extract and print the category codes and labels.
📋 What You'll Learn
Create a pandas DataFrame called
products with columns 'Product' and 'Category' using exact data.Create a categorical version of the
'Category' column called cat_categories.Extract the category codes into a variable called
category_codes.Extract the category labels into a variable called
category_labels.Print
category_codes and category_labels.💡 Why This Matters
🌍 Real World
Stores and businesses often use category codes to simplify data storage and speed up analysis when working with product categories.
💼 Career
Data analysts and scientists use categorical data types to optimize memory and perform faster grouping and filtering in real datasets.
Progress0 / 4 steps