0
0
Data Analysis Pythondata~30 mins

Why transformation reshapes data for analysis in Data Analysis Python - See It in Action

Choose your learning style9 modes available
Why transformation reshapes data for analysis
📖 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.