Aggregations
Last updated
An aggregation turns many numbers into one: a total, an average, a biggest. In this lesson you'll summarise whole arrays and each row or column of a table, find where the biggest value is, and see what a single missing value does to an average.
By the end of this lesson you can
- Summarise an array with
sum,mean,median,std,minandmax - Summarise each row or column with
axis - Find where a value is with
argmaxandargsort, and build a running total withcumsum - Explain
ddof, and average past missing values withnanmean
Collapsing a table
Step through, then run the cells.
Every aggregation takes axis, exactly like sum in lesson 2.4. kilos.mean(axis=1) is each store's average day: Batangas sold 41.25 kg a day, Tanauan 30.25.
The mean is the one from lesson 1.1: add them up and divide by how many there are. For numbers ,
The median is the middle value once they're sorted. With 12 numbers there are two in the middle, 34 and 35, so the median is halfway between them, 34.5.
argmax(axis=1) says where each row's biggest value is, not what it is: Batangas's best day was position 2 (Wednesday), Lipa's position 3 (Thursday).
Where, in order, and so far
One store's week, Monday to Sunday. Run the first two cells.
np.argsort(week)gives the positions that would put the week in order, smallest first. Position 1 (Tuesday, 38 kg) was the quietest day and position 5 (Saturday, 66 kg) the busiest. You'll use this to find the top and bottom of anything, and pandas does the same withsort_valuesin lesson 3.5.np.cumsum(week)is a running total: 42 by Monday night, 80 by Tuesday, 341 by Sunday. It's the answer to "how much so far?".
Standard deviation and ddof
Look at the last two cells: two different standard deviations for the same seven numbers, 9.71 and 10.48.
The standard deviation says how spread out the numbers are: roughly, how far a typical day is from the mean. Its formula has a setting, , "delta degrees of freedom":
ddof=0, NumPy's default, divides by . Use it when the numbers are everything you care about: these seven days, and no others.ddof=1divides by . Use it when the numbers are a sample from something bigger, such as seven days chosen to stand for the whole year. Dividing by a little less makes the answer a little bigger, which makes up for a sample usually being less spread out than the whole.
Here , and the gap between the two is the difference between dividing by 7 and by 6. With 1,000 days, it would hardly show.
This matters because pandas uses ddof=1 by default and NumPy uses ddof=0. The same column gives two different answers depending on which library you ask. Stage 8 explains samples properly; for now, know that the setting exists, and say which one you used.
Wrong way: one nan
Wednesday's figure never arrived, so it's nan. Run the cells.
week.mean() is nan. Any sum with nan in it is nan, so one missing day wipes out the whole week's average, with no error. In a table you'd see a nan in one cell of the results and might not wonder why.
np.nanmean skips the gaps: the mean of the six days there are, 49.33. Every aggregation has a nan version: nansum, nanmax, nanstd, nanmedian.
But skipping isn't always right. If the missing day was the busiest of the week, the average of the rest is too low. So the third cell is the habit to keep: count the gaps before you average past them, and say in the notebook how many there were.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: Statistics: mean, median, std, var, and the nan versions
- NumPy: numpy.std: the formula, and what ddof does
- NumPy: numpy.argsort: the positions that would sort an array
- The Python Standard Library: statistics: pstdev and stdev: the same two formulas in plain Python