0
0
Data Analysis Pythondata~15 mins

replace() for value substitution in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Using replace() for Value Substitution in Data Analysis
📖 Scenario: Imagine you work in a small store. You have a list of products and their categories. Some categories are written in short forms, and you want to replace these short forms with full names to make your data clearer.
🎯 Goal: You will create a dictionary of products with short category codes, then use the replace() method to substitute these codes with full category names.
📋 What You'll Learn
Create a dictionary called products with product names as keys and category codes as values
Create a dictionary called category_map that maps short codes to full category names
Use a dictionary comprehension with replace() to substitute category codes with full names
Print the updated dictionary
💡 Why This Matters
🌍 Real World
Stores and businesses often need to clean and standardize data by replacing codes or abbreviations with full descriptive names for reports and analysis.
💼 Career
Data analysts and scientists frequently use value substitution techniques like replace() to prepare data for visualization and decision-making.
Progress0 / 4 steps
1
Create the products dictionary with short category codes
Create a dictionary called products with these exact entries: 'Apple': 'frt', 'Carrot': 'veg', 'Banana': 'frt', 'Broccoli': 'veg', 'Chicken': 'mt'.
Data Analysis Python
Hint

Use curly braces {} to create the dictionary with product names as keys and short codes as values.

2
Create the category_map dictionary for code substitution
Create a dictionary called category_map with these exact entries: 'frt': 'Fruit', 'veg': 'Vegetable', 'mt': 'Meat'.
Data Analysis Python
Hint

Use curly braces {} to create the dictionary mapping short codes to full names.

3
Use replace() in a dictionary comprehension to substitute codes
Create a new dictionary called full_categories using a dictionary comprehension. For each product and code in products.items(), use code.replace(code, category_map[code]) to replace the short code with the full category name.
Data Analysis Python
Hint

Use a dictionary comprehension with for product, code in products.items() and apply replace() on code.

4
Print the updated dictionary with full category names
Write a print statement to display the full_categories dictionary.
Data Analysis Python
Hint

Use print(full_categories) to show the final dictionary with full category names.