0
0
Pandasdata~30 mins

Aggregation with agg() in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Aggregation with agg()
📖 Scenario: You work in a small store that sells fruits. You have a list of sales data with fruit names, quantities sold, and prices. You want to find out some summary information like total quantity sold and average price for each fruit.
🎯 Goal: Build a program that uses pandas to group the sales data by fruit and then uses agg() to calculate total quantity sold and average price for each fruit.
📋 What You'll Learn
Create a pandas DataFrame with the given sales data
Create a grouping variable for the fruit column
Use agg() to calculate total quantity and average price per fruit
Print the resulting aggregated DataFrame
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarize sales data to understand which products sell the most and at what average price.
💼 Career
Data analysts and scientists use aggregation functions like <code>agg()</code> to quickly get insights from large datasets.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales with these exact columns and values: fruit with ['apple', 'banana', 'apple', 'banana', 'orange'], quantity with [10, 5, 8, 7, 3], and price with [0.5, 0.3, 0.55, 0.35, 0.8].
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Group the data by fruit
Create a variable called grouped that groups the sales DataFrame by the fruit column using groupby().
Pandas
Need a hint?

Use sales.groupby('fruit') to group rows by fruit.

3
Aggregate total quantity and average price
Create a variable called summary that uses agg() on grouped to calculate the total of quantity using 'sum' and the average of price using 'mean'.
Pandas
Need a hint?

Use agg({'quantity': 'sum', 'price': 'mean'}) on the grouped object.

4
Print the aggregated summary
Write a print statement to display the summary DataFrame.
Pandas
Need a hint?

Use print(summary) to show the aggregated results.