0
0
Pandasdata~30 mins

GroupBy with pipe for chaining in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
GroupBy with pipe for chaining
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(grouped_summary) to show the final result.