📖 Scenario: Imagine you have sales data for a small store. The data is messy and hard to analyze because it is not organized well. You want to reshape it so it is easier to see total sales per product and per month.
🎯 Goal: You will reshape the sales data from a list of records into a table format that shows total sales for each product by month. This will help you understand which products sell best in which months.
📋 What You'll Learn
Create a list of dictionaries with sales data including product, month, and sales amount
Create a list of months to use as columns
Use a dictionary comprehension to reshape the data into a nested dictionary with products as keys and monthly sales as values
Print the reshaped data to see the total sales per product per month
💡 Why This Matters
🌍 Real World
Data often comes in messy formats. Reshaping it helps us analyze and understand it better, like seeing sales trends over time.
💼 Career
Data analysts and scientists frequently reshape data to prepare it for reports, dashboards, and decision-making.
Progress0 / 4 steps
1
Create the sales data list
Create a list called sales_data with these exact dictionaries: {'product': 'Apples', 'month': 'Jan', 'sales': 150}, {'product': 'Apples', 'month': 'Feb', 'sales': 200}, {'product': 'Bananas', 'month': 'Jan', 'sales': 100}, {'product': 'Bananas', 'month': 'Feb', 'sales': 120}, {'product': 'Cherries', 'month': 'Jan', 'sales': 75}
Data Analysis Python
Hint
Use a list with dictionaries. Each dictionary has keys 'product', 'month', and 'sales' with the exact values given.
2
Create the list of months
Create a list called months with these exact strings: 'Jan' and 'Feb'
Data Analysis Python
Hint
Just create a list with the two month names as strings.
3
Reshape the sales data using dictionary comprehension
Create a dictionary called reshaped_data using dictionary comprehension. For each unique product in sales_data, create a nested dictionary with keys from months and values as total sales for that product and month. If no sales exist for a month, use 0.
Data Analysis Python
Hint
Use nested dictionary comprehension. The outer dictionary keys are products. The inner dictionary keys are months. Use sum() to add sales matching product and month.
4
Print the reshaped data
Write a print statement to display the reshaped_data dictionary.
Data Analysis Python
Hint
Use print(reshaped_data) to show the final nested dictionary.
Practice
(1/5)
1. Why do we transform data before analysis?
easy
A. To reshape data for easier comparison and visualization
B. To delete important information
C. To make data harder to understand
D. To increase the file size unnecessarily
Solution
Step 1: Understand the purpose of data transformation
Data transformation changes the shape or format of data to make it easier to analyze.
Step 2: Identify the benefit of reshaping data
Reshaping helps in comparing values side-by-side and creating clear visualizations.
Final Answer:
To reshape data for easier comparison and visualization -> Option A
Quick Check:
Transformation = Easier analysis [OK]
Hint: Think: transformation makes data easier to use [OK]
Common Mistakes:
Confusing transformation with deleting data
Believing transformation makes data more complex
Thinking transformation increases file size
2. Which of the following is the correct way to use pandas melt to reshape data?
easy
A. pd.melt(df, id_vars=['score'], value_vars=['id'])
B. pd.melt(df, id_vars=['id'], value_vars=['score'])
C. pd.melt(df, id_vars='score', value_vars='id')
D. pd.melt(df, id_vars=['score', 'id'])
Solution
Step 1: Recall pandas melt syntax
pandas melt requires id_vars as columns to keep and value_vars as columns to unpivot.