0
0
Data Analysis Pythondata~30 mins

Why groupby summarizes data by category in Data Analysis Python - See It in Action

Choose your learning style9 modes available
Why groupby summarizes data by category
📖 Scenario: Imagine you run a small store. You have a list of sales with the product name and the amount sold. You want to find out how much money you made from each product category.
🎯 Goal: You will create a dictionary of sales data, set a category filter, use a loop to sum sales by category, and finally print the total sales per category.
📋 What You'll Learn
Create a dictionary called sales with product names as keys and sales amounts as values
Create a variable called category to select a product category
Use a for loop with variables product and amount to iterate over sales.items()
Sum the sales amounts only for products in the selected category
Print the total sales amount for the selected category
💡 Why This Matters
🌍 Real World
Stores and businesses often need to see total sales by product category to understand which groups sell best.
💼 Career
Data analysts use grouping and summarizing techniques to create reports and insights from sales or customer data.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales with these exact entries: 'apple': 30, 'banana': 15, 'carrot': 10, 'broccoli': 20, 'orange': 25
Data Analysis Python
Hint

Use curly braces {} to create a dictionary with product names as keys and numbers as values.

2
Set the product category to sum
Create a variable called category and set it to the string 'fruit'
Data Analysis Python
Hint

Just assign the string 'fruit' to the variable category.

3
Sum sales for the selected category
Create a variable called total_sales and set it to 0. Then use a for loop with variables product and amount to iterate over sales.items(). Inside the loop, add amount to total_sales only if product is one of 'apple', 'banana', or 'orange' (these are fruits).
Data Analysis Python
Hint

Use total_sales = 0 before the loop. Then check if product is in the list of fruits before adding amount.

4
Print the total sales for the category
Write a print statement to display the text 'Total sales for fruit:' followed by the value of total_sales
Data Analysis Python
Hint

Use print('Total sales for fruit:', total_sales) to show the result.