0
0
NumPydata~30 mins

Why aggregation matters in NumPy - See It in Action

Choose your learning style9 modes available
Why aggregation matters
📖 Scenario: Imagine you work in a small grocery store. You have daily sales data for different fruits. You want to understand the total sales for each fruit to know which ones sell the most.
🎯 Goal: You will create a NumPy array with sales data, set a threshold for high sales, calculate total sales for each fruit, and finally print the fruits that have total sales above the threshold.
📋 What You'll Learn
Use NumPy to handle sales data
Create a 2D NumPy array with exact sales numbers
Set a threshold variable for high sales
Calculate total sales per fruit using aggregation
Print the fruits with total sales above the threshold
💡 Why This Matters
🌍 Real World
Stores and businesses use aggregation to understand total sales, helping them decide what products to stock more.
💼 Career
Data analysts and scientists often aggregate data to summarize and find important insights quickly.
Progress0 / 4 steps
1
Create sales data array
Create a NumPy array called sales with these exact daily sales numbers for fruits (rows are days, columns are fruits): [[10, 5, 8], [7, 3, 6], [12, 4, 9]]
NumPy
Need a hint?

Use np.array() and pass the list of lists exactly as shown.

2
Set sales threshold
Create a variable called threshold and set it to 20 to represent high total sales.
NumPy
Need a hint?

Just assign the number 20 to the variable threshold.

3
Calculate total sales per fruit
Use NumPy's aggregation to create a variable called total_sales that sums sales for each fruit (sum over rows). Use np.sum with axis=0.
NumPy
Need a hint?

Use np.sum with axis=0 to add sales vertically for each fruit.

4
Print fruits with high total sales
Create a list called fruits with these exact names: ["Apples", "Bananas", "Oranges"]. Then print the names of fruits whose total sales in total_sales are greater than threshold. Use a for loop with variables fruit and sale iterating over zip(fruits, total_sales). Print each fruit if sale > threshold.
NumPy
Need a hint?

Use a for loop with zip(fruits, total_sales) and print the fruit name if its sale is greater than threshold.