Learn / NumPy
Intermediate

Aggregations & Statistics

sum, mean, and friends, plus what axis actually means.

axis confuses almost everyone at first. Read it as "the axis that collapses": axis=0 collapses rows (you get one result per column), axis=1 collapses columns (one result per row). No axis at all reduces the whole array to a single number.

FunctionReturnsExample

a.mean(), a.std(), a.var()

average, standard deviation, variance

array([1,2,3]).mean()2.0

a.min(), a.max()

smallest / largest value

array([3,1,2]).max()3

a.argmin(), a.argmax()

position of the smallest / largest value

array([3,1,2]).argmin()1 (index of the 1)

np.median(a), np.percentile(a, 75)

median, 75th percentile

np.median([1,2,3,4])2.5

np.cumsum(a)

running total

np.cumsum([1,2,3])array([1, 3, 6])

💡 Tip: Every aggregation has a NaN-safe twin: nanmean, nansum, nanmax, nanstd, nanmedian, nanquantile. Reach for them whenever missing data is possible.

Now practice it

Exercises that use what this lesson just covered.

Shape Manipulation & BroadcastingRandom Numbers & File I/O