Skip to the workspace
Pebellslearn by building
Exploring and Plotting Data  ›  Lesson

Mean, Median and Mode

Mean, Median, Mode 12 minThe mean is the balance point, the median is the middle of the sorted values, and the mode is the most frequent value, and a gap between mean and median signals skew.

python data analysis / Mean, Median and Mode

Explore each scene at your own pace.

01 · The rule

The rule

Mean, Median and Mode

"What is a typical sale?" sounds like one question with one answer. It is not. There are three common ways to describe the centre of a column, and on the same data they can disagree sharply. Knowing which one to quote is the start of honest analysis.

The rule

Mean balances, median splits, mode repeats.

Back to the clean sales table, with copies 3, 5, 2, 4, 1, 6, 2, 3.

  • Mean: add everything, divide by how many. The balance point. 26 / 8 = 3.25.
  • Median: sort and take the middle. With an even count, average the two middle values. Sorted: 1 2 2 3 3 4 5 6; the middle pair is 3 and 3, so the median is 3.0.
  • Mode: the most frequent value. Here 2 and 3 each appear twice, so there are two modes.
sales["copies"].mean()      # 3.25
sales["copies"].median()    # 3.0
sales["copies"].mode()      # 0    2
                            # 1    3

mode() returns a Series, not one number, precisely because ties happen. It also works on text: sales["genre"].mode() gives fiction and science, tied on three sales each.

Now the lesson that matters. A school orders 60 copies in one bulk sale. Add it, making nine values:

with_bulk = pd.concat([sales["copies"], pd.Series([60])])
with_bulk.mean()      # about 9.56
with_bulk.median()    # 3.0

One unusual sale nearly tripled the mean, while the median did not move. The mean feels every value's size; the median only cares about order.

Each of these works per group too: sales.groupby("branch")["copies"].median().

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.