Statistics along an axis
The same statistic can summarise a whole table, each row, or each column. axis= decides which, and the nan- functions keep a missing reading from spoiling the answer.
- Use
axis=0(down each column) andaxis=1(across each row) withmean,max,sumandstd - Find the position of the largest value with
argmax, and use it to look up a label - Explain why one
nanmakesmeanreturn nan, and usenp.nanmean,np.nanmedianandnp.isnan
With one row per station and one column per day, *the average high* could mean three things: of everything, of each station (across its row), or of each day (down its column). axis= chooses. `axis=1` collapses the columns and leaves one result per row; `axis=0` collapses the rows and leaves one result per column. argmax() returns the position of the largest value instead of the value, which is how you find *which* station or *which* day.
Run each example to see what it prints. Running examples doesn't count toward progress.
import numpy as np stations = ["Wichita", "Orlando", "Sioux Falls"] tmax = np.array([ [-0.5, 6.7, 9.4, 7.2, 1.7], [22.2, 17.8, 21.7, 18.3, 23.3], [1.1, 2.8, -1.0, -1.0, 1.1], ]) station_means = tmax.mean(axis=1) print(station_means.round(2)) print(tmax.max(axis=0)) print(stations[station_means.argmax()]) print(np.median(tmax[0]), tmax[0].std().round(2))
import numpy as np tmax = np.array([[-0.5, 6.7], [22.2, 17.8]]) print(tmax.mean(axis=2))
Real series have gaps. NumPy marks a missing float as np.nan (*not a number*), and any arithmetic with nan gives nan, so one gap makes mean() nan. The nan- functions (np.nanmean, np.nanmedian, np.nansum, np.nanpercentile) skip the gaps, and np.isnan(values) is a mask of where they are. Building an array with dtype=float turns None into nan.
The median (middle value) and percentiles describe skewed data better than the mean: one blizzard drags the mean snowfall up, but barely moves the median.
import numpy as np # Sioux Falls snowfall in mm, 1 to 10 January 2024, and one day with no report snow = np.array([0, 0, 0, 0, 0, 13, 0, 267, 10, 30, None], dtype=float) print(snow.mean()) print(np.isnan(snow).sum(), "missing") print(np.nanmean(snow), np.nanmedian(snow)) print(np.nanpercentile(snow, 90).round(1))
When to use it: per-group summaries of a table (axis=), *which one* questions (argmax, argmin), and series with gaps (nan- functions). Mistakes to avoid: mixing up the axes (check the result's length against the rows or columns you expected), reporting the mean of skewed data without the median, and silently replacing gaps with 0, which changes every statistic.
axis=1gives one result per row,axis=0one per column; no axis summarises everything.argmax/argmingive positions, which index a list of labels.- nan marks a gap; the nan- functions skip gaps, and the median resists outliers.
- Next (the pandas Foundations module): pandas puts labels on the rows and columns, so a table of mixed columns keeps its names.