Overview
Work with a table of sales per store and per day, and pick the right axis to total each store.
- shape gives (rows, columns)
- axis=0 works down the columns
- axis=1 works across the rows
Short Course
A 2D array is a table, and shape gives (rows, columns). Trap: axis=0
collapses the rows, giving one result per column; axis=1 gives one per
row.
sales = np.array([[3, 5, 2], # store A, 3 days
[4, 1, 6]]) # store B
sales.shape # (2, 3)
sales.sum(axis=0) # array([7, 6, 8]): per day
sales.sum(axis=1) # array([10, 11]): per store
Task
Write store_totals(sales) that returns the total of each store (one row per
store), for example store_totals(np.array([[3, 5], [4, 1]])) gives
array([8, 5]).
Test Suites
Pending
Next
Output