📖 Scenario: You work in a small online store. You have a list of orders with customer names and order amounts. You want to learn how to create new features by grouping data and calculating summary numbers like total spending per customer.
🎯 Goal: Build a small program that groups orders by customer and calculates the total amount each customer spent.
📋 What You'll Learn
Create a list of orders with customer names and amounts
Create a variable to hold the minimum amount to consider
Use a dictionary comprehension to sum amounts per customer only if the total is above the minimum
Print the resulting dictionary of customers and their total spending
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarize customer purchases to understand buying habits and target marketing.
💼 Career
Data analysts and data scientists frequently create aggregation features to prepare data for machine learning or reporting.
Progress0 / 4 steps
1
Create the orders list
Create a list called orders with these exact tuples: ("Alice", 50), ("Bob", 20), ("Alice", 30), ("Bob", 40), ("Charlie", 70).
Data Analysis Python
Hint
Use square brackets to create a list. Each order is a tuple with a name and an amount.
2
Set the minimum total amount
Create a variable called min_total and set it to 60.
Data Analysis Python
Hint
Just assign the number 60 to the variable min_total.
3
Calculate total spending per customer above minimum
Use a dictionary comprehension to create a dictionary called total_per_customer. It should sum all amounts per customer from orders and include only customers whose total is greater than or equal to min_total. Use customer and amount as variable names in your comprehension.
Data Analysis Python
Hint
First sum amounts per customer using a loop and a dictionary. Then use a dictionary comprehension to keep only those with total >= min_total.
4
Print the total spending per customer
Write a print statement to display the total_per_customer dictionary.
Data Analysis Python
Hint
Use print(total_per_customer) to show the result.
Practice
(1/5)
1. What is the main purpose of aggregation-based features in data analysis?
easy
A. To summarize data by groups using functions like sum or mean
B. To visualize data using charts and graphs
C. To clean missing values from the dataset
D. To split data into training and testing sets
Solution
Step 1: Understand aggregation-based features
Aggregation-based features summarize data by groups using functions like sum, mean, or count.
Step 2: Compare with other options
Visualization, cleaning, and splitting data are different tasks not related to aggregation features.
Final Answer:
To summarize data by groups using functions like sum or mean -> Option A
Quick Check:
Aggregation = summary by groups [OK]
Hint: Aggregation means grouping and summarizing data [OK]
Common Mistakes:
Confusing aggregation with visualization
Thinking aggregation cleans data
Mixing aggregation with data splitting
2. Which of the following is the correct syntax to calculate the mean of column 'sales' grouped by 'region' in pandas?
easy
A. df.mean('sales').groupby('region')
B. df['sales'].groupby('region').mean()
C. df.groupby('region')['sales'].mean()
D. df.groupby('sales')['region'].mean()
Solution
Step 1: Identify correct groupby syntax
The correct syntax groups by 'region' then selects 'sales' column and applies mean: df.groupby('region')['sales'].mean()
Step 2: Check incorrect options
df.mean('sales').groupby('region') reverses order, df['sales'].groupby('region').mean() tries to group on a column series, df.groupby('sales')['region'].mean() groups by 'sales' instead of 'region'.
Final Answer:
df.groupby('region')['sales'].mean() -> Option C
Quick Check:
Group by region, then mean sales [OK]
Hint: Use df.groupby('col')['col2'].agg_func() [OK]
Common Mistakes:
Calling mean before groupby
Grouping on wrong column
Applying groupby on a Series instead of DataFrame
3. Given the DataFrame:
import pandas as pd
data = {'team': ['A', 'A', 'B', 'B', 'B'], 'points': [10, 15, 10, 5, 20]}
df = pd.DataFrame(data)
result = df.groupby('team').agg({'points': 'sum'})
print(result)
What is the output?
medium
A. team\nA 25\nB 10
B. team\nA 10\nB 20
C. team\nA 15\nB 10
D. team\nA 25\nB 35
Solution
Step 1: Group data by 'team'
Teams are 'A' and 'B'. 'A' has points 10 and 15, 'B' has 10, 5, and 20.
Step 2: Sum points per team
Sum for 'A' = 10 + 15 = 25; Sum for 'B' = 10 + 5 + 20 = 35.
Final Answer:
team
A 25
B 35 -> Option D
Quick Check:
Sum points by team = 25 and 35 [OK]
Hint: Sum points after groupby to get totals per team [OK]
Common Mistakes:
Adding only first value per group
Confusing sum with mean
Misreading group labels
4. What is wrong with this code snippet to get the average 'score' per 'class' in pandas?
df.groupby('class').score.agg('average')
medium
A. The groupby column 'class' is misspelled
B. The aggregation function name 'average' is invalid; should be 'mean'
C. The code should use double brackets: df.groupby(['class'])
D. The code is correct and will run without error
Solution
Step 1: Check aggregation function name
'average' is not a valid pandas aggregation function; the correct function for average is 'mean'.
Step 2: Verify other parts
Grouping by 'class' and accessing 'score' column is correct syntax; double brackets are optional here.
Final Answer:
The aggregation function name 'average' is invalid; should be 'mean' -> Option B
Quick Check:
Use 'mean' not 'average' for average calculation [OK]
Hint: Use 'mean' for average in agg() [OK]
Common Mistakes:
Using 'average' instead of 'mean'
Thinking double brackets are mandatory
Assuming code runs without error
5. You have a sales DataFrame with columns 'store', 'product', and 'revenue'. You want to create a new feature showing the average revenue per store and product combined. Which pandas code correctly creates this aggregation-based feature and adds it as a new column 'avg_rev' to the original DataFrame?
hard
A. df['avg_rev'] = df.groupby(['store', 'product'])['revenue'].transform('mean')
B. df['avg_rev'] = df.groupby('store')['revenue'].mean()
C. df['avg_rev'] = df.groupby('product')['revenue'].sum()
D. df['avg_rev'] = df.groupby(['store', 'product']).agg({'revenue': 'mean'})
Solution
Step 1: Understand the goal
We want average revenue per store and product, and add it as a new column matching original rows.
Step 2: Use groupby with transform
Using groupby on ['store', 'product'] and transform('mean') returns a Series aligned with original DataFrame, perfect for new column.
Step 3: Check other options
df['avg_rev'] = df.groupby('store')['revenue'].mean() groups only by store, not product; df['avg_rev'] = df.groupby('product')['revenue'].sum() sums revenue, not average; df['avg_rev'] = df.groupby(['store', 'product']).agg({'revenue': 'mean'}) returns a DataFrame, not a Series aligned for assignment.
Final Answer:
df['avg_rev'] = df.groupby(['store', 'product'])['revenue'].transform('mean') -> Option A
Quick Check:
Use transform to add group average as new column [OK]
Hint: Use transform for group stats as new column [OK]
Common Mistakes:
Using agg() which returns DataFrame, not Series
Grouping by only one column instead of both
Using sum instead of mean
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.