📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. You want to analyze the sales data to find the average and maximum sales per bread type.
🎯 Goal: Build a pandas DataFrame with sales data, then use groupby and pipe to calculate average and maximum sales per bread type in a clean, chained way.
📋 What You'll Learn
Create a pandas DataFrame with bread sales data
Create a function to calculate average and maximum sales
Use groupby on the bread type
Use pipe to apply the function after grouping
Print the final grouped summary
💡 Why This Matters
🌍 Real World
Grouping and summarizing sales data helps businesses understand product performance and make better decisions.
💼 Career
Data analysts and data scientists often use pandas groupby and pipe to write clear, readable data transformation pipelines.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with these exact columns and values: 'Bread': ['Baguette', 'Baguette', 'Sourdough', 'Sourdough', 'Rye', 'Rye'] 'Sales': [20, 30, 15, 25, 10, 12]
Pandas
Hint
Use pd.DataFrame with a dictionary containing the exact lists for 'Bread' and 'Sales'.
2
Define a summary function
Create a function called summary_stats that takes a DataFrame and returns a new DataFrame with two columns: - 'Average': the mean of the 'Sales' column - 'Max': the maximum of the 'Sales' column
Pandas
Hint
Use df['Sales'].mean() and df['Sales'].max() inside the function and return a DataFrame with these values.
3
Group by bread and apply the function with pipe
Use sales_data.groupby('Bread') and then use pipe(summary_stats) to apply the summary_stats function to each group. Save the result in a variable called grouped_summary.
Pandas
Hint
Use sales_data.groupby('Bread').pipe(lambda g: g.apply(summary_stats).reset_index(level=1, drop=True)) to apply the function to each group and keep the bread names as index.
4
Print the grouped summary
Print the variable grouped_summary to display the average and maximum sales per bread type.
Pandas
Hint
Use print(grouped_summary) to show the final result.
Practice
(1/5)
1. What is the main benefit of using pipe with groupby in pandas?
easy
A. It changes the data type of grouped columns.
B. It automatically sorts the groups in ascending order.
C. It allows chaining custom functions for clearer and cleaner code.
D. It removes missing values from the groups.
Solution
Step 1: Understand the role of pipe
pipe passes the grouped data to a custom function, enabling chaining.
Step 2: Recognize the benefit in code clarity
This chaining makes the code easier to read and maintain by avoiding intermediate variables.
Final Answer:
It allows chaining custom functions for clearer and cleaner code. -> Option C
A. The function multiply does not return the modified group.
B. pipe cannot be used after groupby.
C. The groupby column name is incorrect.
D. DataFrame is missing required columns.
Solution
Step 1: Check the custom function used in pipe
The function multiply modifies the group but does not return it.
Step 2: Understand pipe requires function to return data
Without returning the group, pipe receives None, causing unexpected results or errors.
Final Answer:
The function multiply does not return the modified group. -> Option A
Quick Check:
pipe needs function to return data [OK]
Hint: Always return the modified group in pipe functions [OK]
Common Mistakes:
Forgetting to return the group in the function
Thinking pipe can't follow groupby
Assuming column names are wrong without checking
5. You have a DataFrame with sales data for different stores and want to calculate the average sales per store, then add a column showing sales above average using groupby and pipe. Which code correctly achieves this?
hard
A. df.groupby('Store').pipe(lambda g: g['AboveAvg'] = g['Sales'] - g['Sales'].mean())
B. df.groupby('Store').pipe(lambda gb: gb.apply(lambda g: g.assign(AboveAvg=g['Sales'] > g['Sales'].mean())))
C. df.groupby('Store').pipe(lambda g: g['AboveAvg'] = g['Sales'] > g['Sales'].mean())
D. df.pipe(lambda g: g.groupby('Store').assign(AboveAvg=g['Sales'] > g['Sales'].mean()))
Solution
Step 1: Understand the goal
We want to group by 'Store', then add a column 'AboveAvg' that is True if sales are above the store's average.
Step 2: Analyze each option
df.groupby('Store').pipe(lambda gb: gb.apply(lambda g: g.assign(AboveAvg=g['Sales'] > g['Sales'].mean()))) correctly uses pipe on the grouped object and uses assign to add the column based on group mean. df.pipe(lambda g: g.groupby('Store').assign(AboveAvg=g['Sales'] > g['Sales'].mean())) applies assign before grouping, which is incorrect. Options A and C try to assign inside lambda without returning the DataFrame, causing errors.
Final Answer:
df.groupby('Store').pipe(lambda gb: gb.apply(lambda g: g.assign(AboveAvg=g['Sales'] > g['Sales'].mean()))) -> Option B
Quick Check:
Use pipe with assign inside lambda [OK]
Hint: Use assign inside pipe lambda to add columns [OK]
Common Mistakes:
Assigning inside lambda without returning DataFrame
Applying assign before groupby
Using assignment (=) inside lambda without returning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.