📖 Scenario: You work in a small retail store. You have sales data for different products sold over several days. You want to find out the total sales for each product to understand which products sell the most.
🎯 Goal: Create a program that uses pandas to aggregate sales data by product and report the total sales for each product.
📋 What You'll Learn
Create a pandas DataFrame with sales data for products and their sales amounts.
Create a variable to hold the column name to group by.
Use pandas aggregation to sum sales for each product.
Print the aggregated total sales per product.
💡 Why This Matters
🌍 Real World
Retail stores and businesses often need to summarize sales data to understand product performance and make decisions.
💼 Career
Data aggregation is a fundamental skill for data analysts and data scientists to prepare reports and insights from raw data.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with two columns: 'Product' and 'Sales'. Use these exact rows: 'Apple', 10, 'Banana', 15, 'Apple', 5, 'Orange', 8, 'Banana', 7.
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Set the grouping column
Create a variable called group_column and set it to the string 'Product' to specify the column to group by.
Pandas
Hint
Just assign the string 'Product' to the variable group_column.
3
Aggregate total sales by product
Create a new DataFrame called total_sales by grouping sales_data using group_column and summing the 'Sales' values for each product.
Pandas
Hint
Use groupby() on sales_data with group_column, then select 'Sales' and apply sum().
4
Print the total sales report
Print the total_sales DataFrame to display the total sales for each product.
Pandas
Hint
Use print(total_sales) to show the result.
Practice
(1/5)
1. What is the main purpose of using groupby() in pandas for data aggregation reporting?
easy
A. To filter rows based on a condition
B. To sort data in ascending order
C. To split data into groups based on column values
D. To merge two dataframes
Solution
Step 1: Understand the role of groupby()
The groupby() function splits the dataframe into groups based on unique values in one or more columns.
Step 2: Identify the purpose in aggregation
After grouping, aggregation functions like sum or mean can be applied to each group separately.
Final Answer:
To split data into groups based on column values -> Option C
Quick Check:
groupby() splits data for aggregation [OK]
Hint: Remember: groupby splits data, agg summarizes groups [OK]
Common Mistakes:
Confusing groupby with sorting
Thinking groupby filters rows
Mixing groupby with merging dataframes
2. Which of the following is the correct syntax to calculate the mean of column 'Sales' grouped by 'Region' in pandas?
easy
A. df.groupby('Region').agg('Sales'.mean)
B. df.group_by('Region').mean('Sales')
C. df.groupby('Sales')['Region'].mean()
D. df.groupby('Region')['Sales'].mean()
Solution
Step 1: Identify correct groupby syntax
The correct method is groupby('Region') to group by 'Region'.
Step 2: Select column and apply mean
Use ['Sales'].mean() to calculate mean of 'Sales' per group.
Final Answer:
df.groupby('Region')['Sales'].mean() -> Option D
Quick Check:
Correct method chaining for groupby and mean [OK]
Hint: Use df.groupby('col')['col2'].mean() for grouped mean [OK]
Common Mistakes:
Using group_by instead of groupby
Swapping group and aggregation columns
Incorrect agg syntax with string method
3. Given the dataframe:
import pandas as pd
data = {'Team': ['A', 'A', 'B', 'B', 'C'], 'Points': [10, 15, 10, 20, 30]}
Hint: Sum points after groupby sums values per group [OK]
Common Mistakes:
Summing only first value per group
Confusing sum with mean
Expecting agg() needs list always
4. You run this code:
df.groupby('Category').agg('sum', 'Price')
But get an error. What is the problem?
medium
A. agg() expects a dictionary or list, not multiple arguments
B. groupby() requires numeric columns only
C. sum is not a valid aggregation function
D. Price column does not exist
Solution
Step 1: Check agg() function signature
The agg() method expects a single function, list of functions, or dict mapping columns to functions.
Step 2: Identify incorrect argument usage
Passing two separate arguments ('sum', 'Price') is invalid syntax and causes error.
Final Answer:
agg() expects a dictionary or list, not multiple arguments -> Option A
Quick Check:
agg() needs one argument: function or dict [OK]
Hint: Use agg({'Price': 'sum'}) or agg('sum') only [OK]
Common Mistakes:
Passing multiple arguments to agg()
Assuming groupby needs numeric columns only
Using invalid function names
5. You have a sales dataframe with columns 'Store', 'Product', and 'Revenue'. You want to find the total revenue per store and the average revenue per product within each store. Which pandas code correctly achieves this?
hard
A. df.groupby('Store').agg({'Revenue': ['sum', 'mean']})
B. df.groupby(['Store', 'Product']).agg({'Revenue': 'mean'}).groupby('Store').agg({'Revenue': 'sum'})
C. df.groupby('Store').agg({'Revenue': 'sum'}).groupby('Product').agg({'Revenue': 'mean'})
D. df.groupby('Product').agg({'Revenue': ['sum', 'mean']})
Solution
Step 1: Calculate average revenue per product within each store
Group by both 'Store' and 'Product', then calculate mean revenue.
Step 2: Calculate total revenue per store
Group the result by 'Store' and sum the mean revenues of products.
Final Answer:
df.groupby(['Store', 'Product']).agg({'Revenue': 'mean'}).groupby('Store').agg({'Revenue': 'sum'}) -> Option B
Quick Check:
Nested groupby for product mean then store sum [OK]
Hint: Group by both, then aggregate stepwise for nested summaries [OK]
Common Mistakes:
Trying to do sum and mean in one groupby without nesting
Grouping by wrong columns order
Mixing aggregation levels incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.