NumPy
2 min read
Updated 4 Aug 2026
9. Aggregations
axis=0 collapses rows (result has one value per column). axis=1 collapses columns (one value per row).
| Function | Meaning |
|---|---|
sum, prod |
total / product |
mean, median |
central tendency |
std, var |
spread |
min, max |
extremes |
percentile, quantile |
position statistics |
cumsum, cumprod |
running totals |
The axis parameter (crucial!)#
axis=0 collapses rows (result has one value per column). axis=1 collapses columns (one value per row).
m = np.array([[1, 2, 3],
[4, 5, 6]])
m.sum() # 21 (all elements)
m.sum(axis=0) # [5 7 9] (down columns)
m.sum(axis=1) # [ 6 15] (across rows)
m.mean(axis=0) # [2.5 3.5 4.5]
np.percentile(m, 50) # 3.5 (median)
np.percentile(m, [25, 75]) # [2.25 4.75]
keepdims for broadcasting-friendly results#
col_sum = m.sum(axis=1, keepdims=True) # shape (2,1) instead of (2,)
m / col_sum # row-normalize (broadcasts cleanly)
📌 Remember:
axis=0→ "for each column, go down the rows." Think of the axis as the one being eliminated.
NaN-aware reductions (real data has holes)#
A single NaN poisons a normal reduction (np.mean([1, np.nan]) → nan). Use the nan* variants to skip missing values:
a = np.array([1.0, 2.0, np.nan, 4.0])
np.mean(a) # nan ⚠️ one NaN poisons the whole result
np.nanmean(a) # 2.333 (ignores NaN)
np.nansum(a) # 7.0
np.nanmax(a), np.nanstd(a) # NaN-safe extremes / spread
np.isnan(a).sum() # 1 — count the holes
a[~np.isnan(a)] # drop NaNs before other ops
⚠️ Common Mistake:
np.stduses population std (ddof=0) by default; Pandas.std()uses sample std (ddof=1). Setddof=1in NumPy to match statistics conventions.
⭐ Interview Question: What does
axis=1mean? Aggregate along columns → collapse each row to a single value.