0
0
Data Analysis Pythondata~30 mins

agg() for multiple aggregations in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Using agg() for Multiple Aggregations in Data Science
📖 Scenario: You work in a small grocery store. You have sales data for different fruits sold in the last week. You want to find out some useful information like the total quantity sold, the average price, and the highest price for each fruit.
🎯 Goal: Build a program that uses the agg() function to calculate multiple summary statistics (total quantity, average price, maximum price) for each fruit in the sales data.
📋 What You'll Learn
Create a pandas DataFrame with fruit sales data including columns: 'Fruit', 'Quantity', and 'Price'.
Create a variable to hold the aggregation functions using agg().
Use groupby() on the 'Fruit' column and apply agg() to calculate total quantity, average price, and maximum price.
Print the resulting aggregated DataFrame.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarize sales or inventory data by categories to make decisions.
💼 Career
Data analysts and scientists use groupby and agg functions to quickly get insights from large datasets.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales with these exact columns and values: 'Fruit' with ['Apple', 'Banana', 'Apple', 'Banana', 'Orange'], 'Quantity' with [10, 5, 7, 3, 8], and 'Price' with [0.5, 0.3, 0.55, 0.35, 0.8].
Data Analysis Python
Hint

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

2
Create the aggregation configuration
Create a variable called agg_funcs that holds a dictionary to specify these aggregations: 'Quantity' should use 'sum', and 'Price' should use ['mean', 'max'].
Data Analysis Python
Hint

Use a dictionary where keys are column names and values are aggregation functions or lists of functions.

3
Group by fruit and apply multiple aggregations
Use groupby() on the sales DataFrame by the 'Fruit' column and apply agg() with the agg_funcs dictionary. Store the result in a variable called summary.
Data Analysis Python
Hint

Use sales.groupby('Fruit').agg(agg_funcs) to get the grouped summary.

4
Print the aggregated summary
Print the summary DataFrame to display the total quantity, average price, and maximum price for each fruit.
Data Analysis Python
Hint

Use print(summary) to show the grouped aggregation results.