Loading
0x30Lesson 4 of 10

Summarize and compare groups

Describe the center and spread of a distribution, and compare groups with groupby.

22 min 6-question quiz 3 code exercises
By the end of this lesson you can
  • Choose between mean and median
  • Describe spread with standard deviation and IQR
  • Summarize groups with pandas groupby

A distribution is the full picture of a variable’s values. Summaries compress it into a few numbers:

  • Center - the mean (sum ÷ count) uses every value, so one extreme value can drag it; the median (middle value when sorted) barely moves. For skewed data like incomes, trip times or house prices, report the median.
  • Spread - the standard deviation is roughly the typical distance from the mean; the IQR (Q3 − Q1) is the width of the middle half, and like the median it resists outliers.

Always report the count too: an average of 3 trips means much less than an average of 3,000.

Try it

Up to 15 XP

Drag the distribution around

These are ten commute times. Click far to the right to add a very long commute, and watch which numbers move: the mean and standard deviation jump; the median and IQR barely change. Then remove points by clicking them.

mean030609012012 min - click to remove15 min - click to remove18 min - click to remove20 min - click to remove21 min - click to remove22 min - click to remove25 min - click to remove27 min - click to remove30 min - click to remove75 min (outlier) - click to remove

Click the line to add a value; click a dot to remove it. Red dots are outliers by Tukey’s rule (more than 1.5 × IQR beyond the box).

Mean

26.5 min

Median

21.5 min

Std. deviation

17.9 min

IQR (Q3 − Q1)

10.5 min

n = 10

Comparing groups

Most questions compare groups: members vs casual riders, this month vs last. pandas does it with split-apply-combine:

trips.groupby("mode")["minutes"].agg(["count", "mean", "median"])

A difference between group summaries describes these observations. Whether it’s bigger than chance, or caused by the grouping, are separate questions you’ll meet later in the track.

mean_vs_median.py
1import statistics
2values = [4, 7, 8, 30]
3print("mean:", statistics.mean(values))
4print("median:", statistics.median(values))
Output
mean: 12.25
median: 7.5

Key takeaways

  • Report center, spread and count together.

  • Use the median and IQR for skewed data or data with outliers.

  • groupby + agg summarizes every group at once.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Python

Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.

Exercise 1

Calculate a median

+25 XP

Read a JSON array of an odd number of numeric values, sort it, and print its median.

  • Unsorted values
  • Negative values
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

Standard deviation by hand

+25 XP

Read a JSON array of numbers. Without the statistics module, compute the mean and the sample standard deviation (divide the sum of squared deviations by n − 1, then take the square root). Print mean=M sample sd=S (2 decimals), then matches statistics.stdev: True if your result equals statistics.stdev to 9 decimal places.

  • Eight values
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 3

Summarize each group

+25 XP

Read a CSV with mode and minutes. For each mode (alphabetically), print mode n=N mean=M median=D with the mean and median to 1 decimal.

  • Three modes
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…