Aggregations: sum, mean, axis
An aggregation is a NumPy operation that reduces an array of many values into a single summary statistic — sum, mean, minimum, maximum, or standard deviation — all computed at C speed.
Learn Aggregations: sum, mean, axis in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll master the core aggregation methods and the powerful axis argument that lets you summarize a 2D array per-column or per-row, plus argmax, argmin, and cumulative sums.
What You'll Learn in This Lesson
1 The Core Aggregations
Every NumPy array carries a set of methods that collapse it to a single number. The most common are sum() , mean() , min() , max() , and std() (standard deviation). Called with no arguments, they work across the entire array.
Expected output: 30 , 6.0 , 2 , 10 , and 2.8284271247461903 for the standard deviation.
2 The axis Argument: 0 vs 1
With a 2D array you usually want a result per column or per row , not one number for everything. The axis argument controls this. The simplest rule: axis is the dimension that disappears .
- axis=0 collapses the rows (goes down each column) → one value per column
- axis=1 collapses the columns (goes across each row) → one value per row
Expected output: 21 for the whole grid, [5 7 9] per column (axis=0), and [6 15] per row (axis=1).
3 Mean Per Column and Per Row
The axis argument works the same way for every aggregation. Here we compute the average of each subject (column) and each student (row) from a small grade table.
Expected output: subject averages [83.33 80. 76.67] , student averages [80. 70. 90.] , overall max 100 , and the per-student minimums [70 60 80] .
4 argmax, argmin, and cumsum
Sometimes you need the position of an extreme value, not the value itself. np.argmax returns the index of the largest element and np.argmin the index of the smallest. cumsum gives a running total — each element is the sum of everything up to and including it.
Expected output: 3 , 2 , the running total [12 57 64 152 175] , and [1 2] for the per-row argmax.
🎯 YOUR TURN: Fill in the Blanks
Replace each ___ so the program reports the total of each column and the average overall.
Expected output: [ 90 120] and 35.0 . (Answers: 0 , mean .)
⚠️ Common Errors & Quick Reference
❌ Got per-row results when you wanted per-column
You passed the wrong axis — they are easy to swap.
✅ Fix: remember axis is the dimension that disappears. Use axis=0 for per-column, axis=1 for per-row.
max returns the value; argmax returns the index.
✅ Fix: use argmax / argmin only when you need the position of the extreme value.
Task
Code
Total of whole array
arr.sum()
Per-column sum
arr.sum(axis=0)
Per-row mean
arr.mean(axis=1)
Index of max
np.argmax(arr)
Running total
np.cumsum(arr)
🏆 Mini Challenge: Best Performing Store
Each row is a store and each column is a month. Find the total revenue per store, then identify which store earned the most.
❓ Frequently Asked Questions
Lesson 11 complete — you can summarize any array!
You now reach for sum , mean , min , max , and std with confidence, control direction with axis , and locate extremes with argmax and argmin .
🚀 Up next: Stacking & Splitting — combine and divide arrays along any axis.
Practice quiz
What does np.array([2, 4, 6, 8, 10]).sum() return?
- 20
- 30
- 6.0
- 10
Answer: 30. Adding 2+4+6+8+10 gives 30.
For grid = [[1, 2, 3], [4, 5, 6]], what is grid.sum(axis=0)?
axis=0 collapses the rows, giving one column sum each: [5 7 9].
For grid = [[1, 2, 3], [4, 5, 6]], what is grid.sum(axis=1)?
axis=1 collapses the columns, giving one row sum each: 6 and 15.
Which call returns one value per column of a 2D array?
- arr.sum()
- arr.sum(axis=1)
- arr.sum(axis=0)
- arr.cumsum()
Answer: arr.sum(axis=0). axis=0 removes the row dimension, leaving a per-column result.
What does np.argmax(np.array([12, 45, 7, 88, 23])) return?
- 88
- 4
- 3
- 45
Answer: 3. argmax returns the index of the largest value; 88 sits at index 3.
What is the difference between max and argmax?
- They are identical
- max gives the value, argmax gives its index
- max gives the index, argmax gives the value
- argmax only works on 1D arrays
Answer: max gives the value, argmax gives its index. max returns the largest value while argmax returns its position.
What does np.cumsum(np.array([12, 45, 7])) return?
Each element is the running total: 12, 12+45=57, 57+7=64.
What does the mean of [[90,80,70],[60,70,80],[100,90,80]] along axis=1 return?
axis=1 averages across each row (student): 80, 70, and 90.
Called with no axis argument, arr.sum() returns what?
- One total per column
- One total per row
- A 2D array
- A single scalar over the whole array
Answer: A single scalar over the whole array. Without an axis, the aggregation reduces the entire array to one number.
Which method computes the standard deviation of an array?
- arr.std()
- arr.var()
- arr.cumsum()
- arr.argmin()
Answer: arr.std(). std() returns the standard deviation; for [2,4,6,8,10] it is about 2.83.
Continue this course
- Previous: Broadcasting Explained
- Next: Stacking & Splitting Arrays