NumPy · lesson 8 of 13
Parts in this lesson
Akilos.mean(axis=1)2.4: one answer per row
Bkilos.argmax()where the biggest is
Cweek.std(ddof=1)standard deviation of a sample
Dnp.nanmean(week)the mean, skipping nan

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, min and max
  • Summarise each row or column with axis
  • Find where a value is with argmax and argsort, and build a running total with cumsum
  • Explain ddof, and average past missing values with nanmean

Collapsing a table

Step through, then run the cells.

A table summed down each column. Total kilos per day: add each column, top to bottom.423845403533313729303428kilos: 3 stores × 4 daysMonTueWedThuBatangasLipaTanauankilos.sum(axis=0)
1 of 5 Total kilos per day: add each column, top to bottom.
summary.ipynb

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 x1,x2,…,xn,

x¯=1n∑i=1nxi

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.

week.ipynb
  • 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 with sort_values in 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, ddof, "delta degrees of freedom":

s=1n−ddof∑i=1n(xi−x¯)2
  • ddof=0, NumPy's default, divides by n. Use it when the numbers are everything you care about: these seven days, and no others.
  • ddof=1 divides by n−1. 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 n=7, 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.

gap.ipynb

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

sales has shape (5, 364): five stores, a year of days. What does sales.argmax(axis=1) give?
You have 30 days of sales chosen to stand for the whole year. Which standard deviation should you use?
np.array([10, np.nan, 20]).sum() is…

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.

All sources for Simple Data Analysis