Statistics for analysis · lesson 1 of 13
Parts in this lesson
As.mean(), s.median(), s.mode()three kinds of middle
Bs.quantile([0.25, 0.75]), s.std()spread: the middle half, and the typical distance from the mean
Cs.skew(), s.describe()shape, and everything at once

Describing data

Last updated

Before you can say how sure a finding is, you need to say what the finding is, in a number or two. A week of receipts is 1,203 numbers; nobody can read them all. In this lesson you'll summarise them with a middle (mean, median, mode) and a spread (range, IQR, standard deviation), look at their shape, and learn which summary to trust for which shape.

By the end of this lesson you can

  • Compute the mean, median and mode, and say which fits the data
  • Measure spread with the range, the IQR and the standard deviation
  • Describe the shape of a distribution with skew and percentiles

The middle

Stage 8 is about statistics: numbers that describe data, and ways to say how sure you can be about them. It starts with the simplest kind, a summary. The file receipts.csv is one week at the Batangas store, 1 to 7 March 2024: one row per receipt, with the hour, the number of items, the pesos paid and how the customer paid. Run the cells.

describe.ipynb

There are three common ways to say where the middle is:

  • The mean (lesson 1.1), x¯=1n∑i=1nxi: add the 1,203 receipts and divide by 1,203. It's ₱109.22.
  • The median: sort the values and take the one in the middle (or the mean of the two middle ones). Half the receipts are below it and half above. It's ₱95.
  • The mode: the most common value. It's most useful for counts and groups: the most common number of items is 2, and the most common way to pay is cash (830 of 1,203). For pesos, where almost every value is different, the mode means little.

The mean is ₱14 more than the median. Step 3 shows why.

The spread

Two stores could have the same median receipt and very different customers. The spread says how far the values are from each other. Run the cells.

describe.ipynb
  • The range, ₱8 to ₱507, is easy to read but depends on just the two most extreme receipts.
  • The interquartile range (IQR) is the width of the middle half: a quarter of receipts are below the first quartile (₱55) and a quarter above the third quartile (₱145), so the IQR is ₱90. Extreme values don't move it.
  • The standard deviation, ₱71.81, is roughly how far a typical receipt is from the mean. Step through how it's built:
From deviations to the standard deviation. Five receipts: ₱40, ₱75, ₱95, ₱130, ₱210. How spread out are they?₱0₱50₱100₱150₱200receipt 1receipt 2receipt 3receipt 4receipt 5pesos
1 of 5 Five receipts: ₱40, ₱75, ₱95, ₱130, ₱210. How spread out are they?

As a formula, the variance s2 and the standard deviation s of a sample of n values are:

s2=1n−1∑i=1n(xi−x¯)2s=s2

The second cell computes it by hand and gets pandas' answer. Watch out for np.std: by default NumPy divides by n, not n−1, and gives ₱71.78. Pass ddof=1 ("delta degrees of freedom") to match pandas. With 1,203 values the difference is tiny; with 5 it isn't.

Why n−1? The third cell shows it. It takes 10,000 small samples of 5 receipts and works out each sample's variance both ways. Dividing by n gives 4,145 on average, well below the true 5,152, because a sample's values are always closer to their own mean than to the true one. Dividing by n−1 gives 5,182, right on average (the small gap is chance). That's why it's the usual choice when your data is a sample.

Shape and percentiles

Run the cells.

describe.ipynb

The histogram has a long tail to the right: many small receipts, a few big ones. That shape is right-skewed (or positively skewed), and skew() puts a number on it: 0 for a symmetric shape, positive for a tail to the right, negative for a tail to the left. Here it's 1.26, clearly skewed.

In a right-skewed distribution the mean is pulled toward the tail, above the median: the few ₱300 and ₱500 receipts add a lot to the total but only count as "above the middle" for the median. Try it yourself: drag one receipt far to the right.

Mean against mediandrag a receipt, or use the arrow keys
₱0₱200₱400₱600₱800₱1,000median ₱95mean ₱102pesos per receipt

Mean ₱102.42, median ₱95.0. 5 of the 12 receipts are above the mean.

Drag the biggest receipt to ₱1,000. The mean moves a twelfth of the way you drag; the median, the middle of the sorted values, only cares about which receipts are in the middle.

Percentiles describe the shape in more detail: the 90th percentile is the value 90% of receipts are below. Here 10% of receipts are under ₱32, and 10% over ₱201.80; 1% are over ₱317.98. The quartiles are the 25th and 75th percentiles, and the median is the 50th. describe() gives the count, mean, standard deviation and five of them at once.

Which summary fits?

ShapeMiddleSpread
roughly symmetric, one hump, no extreme valuesmean (the median is about the same)standard deviation
skewed, or with extreme values (incomes, receipts, house prices, waiting times)medianIQR, or percentiles
groups or counts (how people paid, items per receipt)mode, and the share in each group—
two humpsneither: describe each group on its own—

For receipts, then, "a typical receipt is ₱95, and the middle half are between ₱55 and ₱145" describes the week better than "the mean is ₱109.22 with a standard deviation of ₱71.81". The mean still matters for one thing: totals. The week's takings are the mean times the number of receipts, so if the owners care about money in the till, the mean is the number to plan with.

Always look at the histogram first. A summary describes a shape, so pick it after you've seen the shape.

Wrong way: one rich household

A report wants to describe the incomes on one street. Run the cells.

income.ipynb

The 50 households have a mean income of ₱22,716 a month and a median of ₱21,700. Then one family with a business earning ₱20 million a month moves in. The mean jumps to ₱414,427, 19 times the median, and 50 of the 51 households are below the mean. The median doesn't move at all.

Nothing went wrong in the code: the mean is computed correctly. It's just the wrong summary. "The average household on this street earns ₱414,427" is true and completely misleading. This is why official income statistics report the median, or the mean together with the median, and why a mean that's far from the median is a warning.

The check: before reporting a mean, compare it with the median. If they're far apart, look at the histogram and the biggest values, and report the median (or both, saying why they differ).

Check what you learned

Waiting times at a bank are mostly 2 to 5 minutes, with a few of an hour. Which describes a typical wait best?
Predict, then run it in a notebook: np.std([2, 4, 4, 4, 5, 5, 7, 9]) and pd.Series([2, 4, 4, 4, 5, 5, 7, 9]).std(). Are they the same?
One wrong value, 9,500 instead of 95, gets into the receipts. Which summary changes most?

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