📖 Scenario: You work in a small store and have a list of sales data for different products. You want to learn how to find useful summary information like the total sales, average sales, and the highest sale for each product.
🎯 Goal: Build a pandas DataFrame with sales data, then use multiple aggregation functions to find the total, average, and maximum sales for each product.
📋 What You'll Learn
Create a pandas DataFrame with product sales data
Create a list of aggregation functions
Use the agg() method with multiple aggregation functions on grouped data
Print the final aggregated DataFrame
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarize sales data to understand performance by product or category.
💼 Career
Data analysts and scientists use grouping and aggregation to prepare reports and find insights from data.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact columns and rows: Product: 'Apple', 'Banana', 'Apple', 'Banana', 'Apple' Sales: 10, 15, 7, 20, 5
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Create a list of aggregation functions
Create a list called agg_functions that contains the strings 'sum', 'mean', and 'max' to use for aggregation.
Pandas
Hint
Make a list with the exact strings 'sum', 'mean', and 'max'.
3
Group by product and apply multiple aggregation functions
Use sales_data.groupby('Product')['Sales'].agg(agg_functions) and save the result in a variable called summary.
Pandas
Hint
Use groupby('Product') on sales_data, select the 'Sales' column, then call agg(agg_functions).
4
Print the aggregated summary
Print the variable summary to display the total, average, and maximum sales for each product.
Pandas
Hint
Use print(summary) to show the results.
Practice
(1/5)
1. What does the agg() function do when used after groupby() in pandas?
easy
A. It merges two DataFrames together.
B. It sorts the groups in ascending order.
C. It filters rows based on a condition.
D. It applies multiple aggregation functions to each group and returns their results.
Solution
Step 1: Understand groupby operation
The groupby() function splits data into groups based on column values.
Step 2: Apply agg() after grouping
The agg() function applies one or more aggregation functions like sum, mean, count to each group and returns the results.
Final Answer:
It applies multiple aggregation functions to each group and returns their results. -> Option D
Quick Check:
agg() summarizes groups [OK]
Hint: Remember: agg() summarizes groups with many functions [OK]
Common Mistakes:
Thinking agg() sorts data
Confusing agg() with filtering
Assuming agg() merges DataFrames
2. Which of the following is the correct syntax to apply both sum and mean aggregations on a grouped DataFrame df grouped by column 'Category'?
easy
A. df.groupby('Category').agg(['sum', 'mean'])
B. df.agg(['sum', 'mean']).groupby('Category')
C. df.groupby('Category').agg('sum', 'mean')
D. df.groupby('Category').aggregate(sum, mean)
Solution
Step 1: Identify correct method chaining
Grouping must happen first with groupby('Category'), then aggregation with agg().
Step 2: Use list of functions inside agg()
The correct syntax uses a list of function names: agg(['sum', 'mean']).
Final Answer:
df.groupby('Category').agg(['sum', 'mean']) -> Option A
A. agg() does not accept multiple functions for different columns.
B. The column names 'Sales' and 'Profit' must be passed as a list, not dict.
C. The code is correct and runs without error.
D. The dictionary mixes single string and list of functions for different columns, which is invalid.
Solution
Step 1: Check agg() dictionary syntax
agg() accepts a dictionary where values can be a string or list of functions per column.
Step 2: Verify code correctness
The code correctly applies 'sum' to 'Sales' and ['mean', 'max'] to 'Profit' without error.
Final Answer:
The code is correct and runs without error. -> Option C
Quick Check:
agg() accepts mixed string and list in dict [OK]
Hint: agg() allows string or list per column in dict [OK]
Common Mistakes:
Thinking mixing string and list causes error
Expecting all functions must be in a list
Confusing agg() with apply() syntax
5. You have a DataFrame df with columns 'Store', 'Sales', and 'Expenses'. You want to group by 'Store' and get the total sales, average expenses, and count of sales entries per store in one step. Which code correctly does this?
hard
A. df.groupby('Store').agg({'Sales': 'sum', 'Expenses': ['mean', 'count']})
B. df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'})
C. df.groupby('Store').agg({'Sales': 'mean', 'Expenses': 'sum', 'Store': 'count'})
D. df.groupby('Store').agg(['sum', 'mean', 'count'])
Solution
Step 1: Identify required aggregations per column
We want total sales (sum), average expenses (mean), and count of sales entries (count).
Step 2: Map functions correctly in agg()
Use a dict: 'Sales' gets ['sum', 'count'], 'Expenses' gets 'mean'. This matches df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'}).
Final Answer:
df.groupby('Store').agg({'Sales': ['sum', 'count'], 'Expenses': 'mean'}) -> Option B
Quick Check:
Dict with list and string per column [OK]
Hint: Use dict with list for multiple funcs per column [OK]
Common Mistakes:
Putting count under wrong column
Using agg() without dict for columns
Applying all functions to all columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.