0
0
Pandasdata~30 mins

Named aggregation in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Summarizing Sales Data with Named Aggregation
📖 Scenario: You work in a small store and have a list of sales records. Each record shows the product sold, the quantity sold, and the price per item. You want to find out the total quantity sold and the average price for each product.
🎯 Goal: Build a pandas DataFrame from the sales data, then use groupby with named aggregation to calculate the total quantity and average price per product.
📋 What You'll Learn
Create a pandas DataFrame called sales with columns product, quantity, and price using the exact data provided.
Create a variable called agg_funcs that holds the named aggregation dictionary for total quantity and average price.
Use sales.groupby('product').agg(**agg_funcs) to group and aggregate the data.
Print the resulting DataFrame.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarize sales data to understand product performance and pricing trends.
💼 Career
Data analysts and data scientists use named aggregation in pandas to create clear, readable summaries of grouped data for reports and decision making.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales with these exact rows: {'product': ['apple', 'banana', 'apple', 'banana', 'orange'], 'quantity': [10, 5, 7, 3, 8], 'price': [0.5, 0.3, 0.55, 0.35, 0.8]}.
Pandas
Need a hint?

Use pd.DataFrame and pass a dictionary with keys 'product', 'quantity', and 'price' and their respective lists.

2
Define the named aggregation dictionary
Create a variable called agg_funcs that holds a dictionary for named aggregation with these keys and values: total_quantity=('quantity', 'sum') and average_price=('price', 'mean').
Pandas
Need a hint?

Define agg_funcs as a dictionary with keys for the new column names and values as tuples of (column, aggregation function).

3
Group and aggregate the sales data
Use sales.groupby('product').agg(**agg_funcs) to group the sales DataFrame by product and apply the named aggregation stored in agg_funcs. Store the result in a variable called summary.
Pandas
Need a hint?

Use groupby('product') and then agg(**agg_funcs) to apply the named aggregation.

4
Print the summary DataFrame
Print the summary DataFrame to see the total quantity and average price for each product.
Pandas
Need a hint?

Use print(summary) to display the grouped and aggregated data.