Summarize and compare groups
Describe the center and spread of a distribution, and compare groups with groupby.
- 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 XPDrag 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.
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
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.
1import statistics
2values = [4, 7, 8, 30]
3print("mean:", statistics.mean(values))
4print("median:", statistics.median(values))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.
Calculate a median
Read a JSON array of an odd number of numeric values, sort it, and print its median.
- Unsorted values
- Negative values
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.
Standard deviation by hand
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
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.
Summarize each group
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
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…