Summing Data with np.sum() and the axis Parameter
📖 Scenario: Imagine you work at a small store that tracks daily sales of different products. You have sales data for 3 products over 4 days. You want to find total sales for each product and total sales for each day.
🎯 Goal: You will create a 2D numpy array with sales data, then use np.sum() with the axis parameter to calculate total sales per product and per day.
📋 What You'll Learn
Create a numpy array called
sales with the exact data given.Create a variable called
axis_0 set to 0 to represent summing over rows (days).Use
np.sum() with axis=axis_0 to find total sales per product.Print the total sales per product.
Create a variable called
axis_1 set to 1 to represent summing over columns (products).Use
np.sum() with axis=axis_1 to find total sales per day.Print the total sales per day.
💡 Why This Matters
🌍 Real World
Stores and businesses often track sales data in tables and need to quickly find totals by product or by day to understand performance.
💼 Career
Data analysts and scientists use numpy and its sum function with axis to summarize and analyze multi-dimensional data efficiently.
Progress0 / 4 steps