Lesson 3 - The Mode
Welcome to the Mode
You have met the mean and the median — two ways to find the center of a column of numbers. But what is the “average” color of a car, or the typical country a car comes from? You cannot add up “USA” and “Japan” and divide by two, and you cannot put colors in order to find a middle one. For data like this, the only sensible measure of center is the mode — the value that simply shows up most often.
In this lesson you will compute the mode on a real dataset of cars, see why it is the only average that works for categories, and learn how to recognize when a distribution has one peak, two, or several. Then you will pull all three measures of center together into a single decision guide: given a column, which average should you actually report?
By the end of this lesson, you will be able to:
- Define the mode and compute it with
.mode()andvalue_counts() - Explain why the mode is the only measure of center valid for nominal data
- Tell unimodal, bimodal, and multimodal distributions apart
- Choose between the mean, median, and mode based on a variable’s scale and shape
You only need a little Python and pandas. Let’s begin.
What the Mode Is
The mode is the most frequently occurring value in a dataset. Where the mean balances the values and the median sits in the middle of the sorted order, the mode answers a different question entirely: which value happens most?
Load the cars dataset and look at the cylinders column, which records how many cylinders each car’s engine has:
import pandas as pd
cars = pd.read_csv("https://datatweets.com/datasets/cars.csv")
print(cars["cylinders"].value_counts())cylinders
4 204
8 103
6 84
3 4
5 3
Name: count, dtype: int64value_counts() tallies how often each value appears, sorted from most to least common. The very first row tells you the answer: 4-cylinder engines appear 204 times, more than any other count. So the mode of cylinders is 4.
You do not have to read it off by eye. pandas has a dedicated method:
print(cars["cylinders"].mode())0 4
dtype: int64.mode() returns the most frequent value (or values — more on that soon) as a Series. To pull out the single number, index into it:
print(cars["cylinders"].mode()[0])4Unlike the mean, the mode is always an actual value that exists in the data. The mean number of cylinders works out to a fractional 5.45, an engine that no car in the world has. The mode is always real: some car really does have 4 cylinders.
Here is the cylinder distribution as a chart, with the modal bar — the tallest one — picked out in a darker shade:
The Only Average for Categories
The mean and the median both need numbers. To average values you must add them; to find a median you must order them. The mode needs neither — it only needs to count. That makes it the only measure of center you can use on nominal data: categories with no inherent order and no arithmetic, like color, brand, or country.
In module 1 you learned that the origin column is nominal — it labels where a car was built, with no natural ranking between the labels. Try to take its mean and pandas will refuse, because you cannot do arithmetic on words. But you can absolutely ask which label is most common:
print(cars["origin"].value_counts())origin
usa 249
japan 79
europe 70
Name: count, dtype: int64print(cars["origin"].mode()[0])usaThe mode of origin is usa: of 398 cars, 249 were built in the United States, more than Japan and Europe combined. That is a perfectly meaningful “typical value” — and it is the only measure of center this column can have. There is no mean origin and no median origin; there is only a most common one.
The mode is the workhorse for categories
Whenever someone reports “the most popular plan,” “the best-selling color,” or “the most common diagnosis,” they are reporting a mode. For nominal data it is not just the best choice of center — it is the only one available.
One Peak or Many: Modality
The mode is not always a single value. When you describe the shape of a distribution by counting its peaks, you are describing its modality.
A unimodal distribution has one clear peak — one value (or region) that is most common. The cylinders column is unimodal: 4-cylinder cars tower over everything else.
A bimodal distribution has two peaks of roughly equal height, and a multimodal distribution has several. This matters because a single mode can hide a split population. If a dataset of customer ages peaked at both 20 and 60, reporting a single “typical age” of 40 would describe almost nobody.
pandas handles ties honestly. When two or more values are equally the most frequent, .mode() returns all of them:
prices = pd.Series([5, 5, 7, 7, 9])
print(prices.mode())0 5
1 7
dtype: int64Here both 5 and 7 appear twice, so the series is bimodal and .mode() reports both. This is exactly why .mode() returns a Series rather than a single number: it cannot assume there is only one answer. Always be ready for the possibility of more than one mode, and check the length when it matters:
print(len(cars["cylinders"].mode()), "mode(s)")1 mode(s)The cylinder column has exactly one mode, confirming it is unimodal.
The Mode for Continuous Data
The mode shines on categories and on numbers that take only a few distinct values, like cylinders. It gets slippery on continuous data — measurements like fuel economy that can take many fine-grained values.
Look at mpg. Across 398 cars there are 129 distinct mpg values, so any single one is shared by only a handful of cars:
print(cars["mpg"].nunique(), "distinct mpg values")
print("raw mode:", cars["mpg"].mode()[0])129 distinct mpg values
raw mode: 13.0The “mode” comes out to 13.0 mpg — but that value appears only because 20 cars happen to land on it exactly, and dozens of other values appear nearly as often. A mode built on a near-tie like this is fragile: collect a few more cars and it could jump somewhere else. For truly continuous data, the single most frequent value is rarely a useful summary.
The fix is the one you met in module 1: bin the values into intervals and find the modal class — the interval that contains the most observations. Group mpg into 5-wide bins and count them:
bins = pd.cut(cars["mpg"], bins=range(8, 49, 5))
print(bins.value_counts().sort_index())mpg
(8, 13] 33
(13, 18] 91
(18, 23] 83
(23, 28] 83
(28, 33] 57
(33, 38] 38
(38, 43] 7
(43, 48] 6
Name: count, dtype: int64The fullest interval is (13, 18] with 91 cars, so that range is the modal class: the typical car of this era got somewhere between 13 and 18 mpg. This is far steadier than the raw mode of 13.0, because it pools nearby values instead of betting everything on one exact number. The lesson carries straight over from the grouped frequency distributions you built earlier: for continuous data, the mode lives in an interval, not a point.
Choosing Mean, Median, or Mode
You now have three measures of center. The skill is not computing them — pandas does that in a line each — but knowing which one to report. Two questions decide it: what scale is the variable measured on, and what shape does its distribution have.
By measurement scale
The scale of a variable controls which averages are even allowed, an idea from module 1:
- Nominal (unordered categories like
origin): only the mode. You cannot add or order labels, so the mean and median are undefined. - Ordinal (ordered categories, like small/medium/large): the median or the mode. You can order the values, so a middle one exists, but the gaps between them are not equal, so the mean is unreliable.
- Interval or ratio (true numbers, like
mpgorweight): any of the three. Here the choice comes down to shape.
By shape (skew)
Once a variable is numeric, the deciding factor is whether its distribution is symmetric or skewed. Recall from the median lesson that the mean is pulled toward a long tail while the median resists it.
- Symmetric distributions: prefer the mean. The values balance, the mean and median nearly coincide, and the mean uses every data point.
- Skewed distributions: prefer the median. It marks the true center while the mean drifts toward the tail and misrepresents the typical case.
Compare the cars’ two key numeric columns:
print("mpg mean:", round(cars["mpg"].mean(), 2), "median:", cars["mpg"].median())
print("weight mean:", round(cars["weight"].mean(), 1), "median:", cars["weight"].median())mpg mean: 23.51 median: 23.0
weight mean: 2970.4 median: 2803.5For mpg the mean (23.51) and median (23.0) sit almost on top of each other — a roughly symmetric column where the mean is a fine choice. For weight the mean (2970.4) is a clear 167 pounds above the median (2803.5), the signature of a right skew: a cluster of heavy cars drags the mean up. There the median is the more honest “typical car.”
A recommendation table
| Variable type | Distribution shape | Report |
|---|---|---|
Nominal (e.g. origin) | — | Mode only |
| Ordinal (e.g. size S/M/L) | — | Median (or mode) |
Interval / ratio (e.g. mpg) | Symmetric | Mean |
Interval / ratio (e.g. weight) | Skewed | Median |
| Any | Need the most common value | Mode |
Report more than one when in doubt
These are defaults, not laws. When a distribution is skewed or has multiple peaks, the most honest summary often shows the mean and the median together — the gap between them tells the reader how skewed the data is at a glance.
Practice Exercises
Exercise 1: Find the most common model year
The model_year column records the year each car was made (as a two-digit number). Find its mode — the single year that produced the most cars in this dataset — and how many cars that was.
Hint
Use cars["model_year"].mode()[0] for the value, then cars["model_year"].value_counts() to read off how many cars share that year. The top row of value_counts() is the mode.
Exercise 2: A mode for a category
The origin column is nominal, so the mode is its only valid measure of center. Confirm the mode is usa, then compute what fraction of all cars are American-made. Why can you not compute a mean or median for this column?
Hint
Pass normalize=True to see proportions: cars["origin"].value_counts(normalize=True). The mean and median are undefined because the labels have no numeric value and no inherent order.
Exercise 3: Choose the right measure for displacement
The displacement column (engine size) is a ratio variable. Compute its mean and median, decide from the gap between them whether the distribution looks skewed, and state which measure of center you would report and why.
Hint
Compute cars["displacement"].mean() and cars["displacement"].median(). If the mean sits well above the median, the column is right-skewed and the median is the safer summary; if they are close, the mean is fine.
Summary
The mode is the most frequent value in a dataset, and the one measure of center that needs nothing but counting — which is why it is the only average that works for nominal categories like origin (mode usa). You compute it with .mode() or read it off the top of value_counts(), and you stay alert to modality: a distribution can be unimodal, bimodal, or multimodal, and .mode() will return every tied value. For continuous data the single most frequent point is fragile, so you bin the values and report the modal class instead. Finally, choosing between mean, median, and mode comes down to a variable’s scale (nominal forces the mode; ordinal allows the median) and its shape (symmetric favors the mean; skewed favors the median).
Key Concepts
- Mode — the most frequently occurring value in a dataset.
.mode()— thepandasmethod that returns the most frequent value(s) as aSeries.- Modality — the number of peaks in a distribution: unimodal, bimodal, or multimodal.
- Modal class — the interval containing the most observations, used as the mode for binned continuous data.
- Nominal data — unordered categories for which the mode is the only valid measure of center.
- Choosing a center — nominal → mode; ordinal → median or mode; symmetric numeric → mean; skewed numeric → median.
Why This Matters
Every dashboard and report quietly decides which “average” to show, and the wrong choice can mislead an entire audience. Reporting a mean salary in a company with a few enormous earners, or a single “typical age” for a clearly two-peaked customer base, turns a true number into a false impression. Knowing that the mode is the right tool for categories, the median for skew, and the mean for symmetry is what lets you summarize data without distorting it.
Next Steps
Continue to Lesson 4 - Measures of Variability
A center alone can lie. Learn the range, variance, and standard deviation that measure how spread out your data really is.
Back to Module Overview
Return to the Measures of Center & Variability module overview
Continue Building Your Skills
You can now report the right center for any column — the mode for categories, the median for skew, the mean for symmetry. But a center is only half the story. Two columns can share the very same average and still behave completely differently, depending on how tightly their values cluster. Next you will learn to measure that spread.