…
…
Statistics and Data Analysis Fundamentals · Lesson 12 / 44 · Distribution and center
PreviewIn this lesson you will
You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
Management asks, "What is our average order amount?" The mean is $75.53, but half of the orders are $44.95 or less, and both numbers are correct. In this lesson you will measure the center of a distribution with the mean, the median, and the mode, and choose the one that fits the question.
At the end of the lesson you will write a two-line summary for management like this:
OutputThe average basket is $75.53; half of the orders are $44.95 or less.
The gap shows that a few large orders pull the average up.In this lesson you will:
In lesson 11 you drew a histogram of the order amounts at Bazaar. Most of the bars sit on the left, below about $50. A thin tail stretches to the right, up to about $900. A histogram shows the whole picture of a distribution. In a meeting or a report, though, you often need a single number that answers "How big is a typical order?" A measure of center answers this question. There are three classic measures of center: the mean, the median, and the mode. All three define "the value in the middle" in a different way, and they can give different numbers for the same data. You do not ask which one is correct. You ask which one answers your question.
The arithmetic mean is the sum of the values divided by the number of observations. Think of five friends and how many books each one read in a week: 2, 3, 3, 4, and 18 (the last one finishes a book every day on vacation). The total is 30, there are 5 people, and the mean is 30 / 5 = 6. An important property of the mean is that it is tied to the total: mean × number of people = total (6 × 5 = 30). It answers the question, "If everyone read the same amount, how many books would each person have read?"
The median is the value that stays in the exact middle when the values are sorted from smallest to largest. Half of the observations are below the median and half are above it. With five values, it is the third one: 2, 3, 3, 4, 18 → median 3. If the number of observations is even, there is no single value in the exact middle. You take the mean of the two middle values. For example, for 2, 3, 4, 18 the two middle values are 3 and 4, so the median is 3.5.
The values are 2, 3, 3, 4, 18. What are the mean and the median?
Two mistakes are common with the median. The first is to think that the median is the middle of the smallest and the largest value: (2 + 18) / 2 = 10. This number looks only at the two extreme values and ignores everything between them. The second is to take the middle element of the list without sorting it. If the same five values were written in the order [18, 3, 2, 4, 3], the middle element would be 2, but the median is still 3. The first step of the median is always sorting. Follow the code below step by step: first the list is sorted, then the middle element (or the two middle elements) is found based on the length. Change the list (for example, delete the last value so that an even number of elements remains) and trace it again.
Run the code line by line: see which line runs next, the variable boxes and the output.
x = [18, 3, 2, 4, 3] s = sorted(x) n = len(s) mid = n // 2 if n % 2 == 1: median = s[mid] else: median = (s[mid - 1] + s[mid]) / 2 print("Sorted:", s) print("Index of the middle element:", mid) print("Median:", median)
In the code, n // 2 gives the whole part of the division and n % 2 gives the remainder. With five elements, mid is 2, and because Python indexes start at 0, s[2] is the third element. If the remainder is 1, the number of elements is odd and there is a single middle value. If the remainder is 0, there are two middle values and you take their mean. pandas does this for you: median() sorts first and then finds the middle.
Think of the mean as the balance point of a seesaw. The values are children sitting on the board, and the mean is the point where the board stays balanced. If a very heavy person sits near one end, the balance point moves toward that person. The median does not look at the weights. It looks only at the order.
In the code below, make the last value larger and watch how the mean and the median react:
What happens to the mean and the median as the last value gets larger?
Pick a value; predict the output first, then look.
1import pandas as pd2last_value = 183x = pd.Series([2, 3, 3, 4, last_value])4print("Mean:", x.mean())5print("Median:", x.median())
Mean: 6.0 Median: 3.0
Faded lines didn't run with this value. A single extreme value pulls the mean wherever it wants: from 3.4 to 362.4. The median stays at 3 every time, because the value in the middle of the order did not change. When the last value is 1800, four of the five people are far below the mean. The mean no longer describes anyone who is typical.
The lesson here is that the mean does not always give the "typical" value. If the values are balanced, the mean sits in the middle. If there are one or more extreme values, the mean shifts toward them, and most observations stay on one side of the mean. This does not mean the mean is wrong. It is still the right tool for a question about a total. It just cannot carry the word "typical."
Now let's return to Bazaar's 885 orders. In pandas, the mean is calculated with mean() and the median with median():
import pandas as pd df = pd.read_csv("orders.csv") print(round(df["amount"].mean(), 2), df["amount"].median()) print(len(df), round(df["amount"].sum(), 2)) print(round(df["amount"].mean() * len(df), 2))
Output75.53 44.95
885 66843.57
66843.57The mean is $75.53 and the median is $44.95. The third line shows where the mean is useful: mean × number of orders gives the total revenue exactly. The same multiplication with the median (885 × $44.95 ≈ $39,800) explains only a little more than half of the revenue. You cannot use the median in a calculation about totals. So what share of the orders is below the mean? Remember the idea from lesson 5: the mean of a True/False column gives the share of True values. Try it yourself:
Try it now· not graded; doesn't affect progress
72.1% of the orders are below the mean. Someone who hears "The average order is $75.53" might think that half of the orders are above it and half below it. In reality, about three out of four orders are smaller. The median carries exactly this "half and half" information.
When the mean and the median come out different, neither one is wrong. The gap tells you about the shape of the distribution. In a symmetric distribution, the values on the two sides balance each other, and the mean and the median fall close together. If there is a long tail on the right, a few large values pull the mean to the right while the median stays in place: the mean is larger than the median. With a tail on the left, it is the other way around. Let's look at three of Bazaar's variables:
import pandas as pd c = pd.read_csv("customers.csv") s = pd.read_csv("sessions.csv") print(round(c["age"].mean(), 1), c["age"].median()) print(round(s["checkout_time_sec"].mean(), 1), s["checkout_time_sec"].median())
Output33.3 33.0
116.3 114.0For customer age, the mean is 33.3 and the median is 33. For the time spent on the checkout page, they are 116.3 and 114 seconds. In both, the two measures are almost the same: the histograms are balanced. For the order amount, the mean ($75.53) is about 1.7 times the median ($44.95). This is the trace in the numbers of the long right tail from lesson 11. When you split the data by category, the gap grows even more in some places:
import pandas as pd df = pd.read_csv("orders.csv") print(df.groupby("category")["amount"].agg(["mean", "median"]).round(2))
Output mean median
category
Beauty 27.89 21.45
Books 21.80 14.90
Clothing 68.53 46.46
Electronics 146.48 70.10
Home & Living 93.10 64.95In Electronics, the mean is more than twice the median. A few very expensive products (for example, orders close to $900) pull the mean up in this category. In Beauty the ratio is smaller (27.89 / 21.45 ≈ 1.3), because the prices are closer to each other. When you read this table, it would be misleading to say "The typical Electronics order is $146.48." The typical order is around $70.
Match each (mean, median) pair with the description of its histogram.
For each row, pick its match from the list, then check.
The mean and the median work with numbers. But what is the center for the question, "Which carrier is used most at Bazaar?" Carrier names cannot be added up and cannot be put in a meaningful order. As you saw in lesson 2, this is a nominal variable. Here the only meaningful measure of center is the mode: the most frequent value. The question "What is the best-selling product in the canteen?" is also a mode question.
import pandas as pd df = pd.read_csv("orders.csv") print(df["carrier"].value_counts()) print(df["carrier"].mode())
Outputcarrier
Arrow Express 350
Gull Post 297
Caravan Freight 238
Name: count, dtype: int64
0 Arrow Express
Name: carrier, dtype: strvalue_counts() gives how many times each value appears, from largest to smallest. The one at the top is the mode. mode() gives the mode directly, but be careful: the result is not a single value. It is a Series with the number 0 on its left. The reason is that several values can be tied at the top with the same frequency. For example, pd.Series([3, 5, 5, 8, 8, 9]).mode() returns two rows: 5 and 8 each appear twice. To get a single value, write mode()[0].
With ordinal variables (lesson 2), the situation is a little different. The satisfaction score in the survey is an ordinal scale from 1 to 5. The values can be sorted, but you cannot say that the "distance" between 4 and 5 is the same as the distance between 2 and 3. Here both the mode and the median are meaningful. The mean (3.93) is often reported, but it is debatable because it assumes equal distances between the scores. If you report it, give it together with the median or a frequency table.
With continuous numeric variables, the mode is often useless: the values almost never repeat. In checkout time, the most frequent value is 103 seconds, but it appears in only 6 of 195 observations, so this number says nothing about the center. In the order amount, the mode of $44.95 (57 orders) is an exception. Prices in the store gather at certain points ($44.95, $59.95, $34.95), so the same amount repeats often.
Try it now· not graded; doesn't affect progress
In satisfaction, the score 4 is the most frequent value with 135 people, and the median is also 4. Both measures support the sentence, "The typical customer gives 4 out of 5."
You now know the three measures. Let's decide which one to use and when. The rule is to look at what the question asks for:
Let's try this rule outside the case. In a small workplace of ten people, nine employees earn 30, 31, 32, 33, 34, 35, 36, 38, and 41 thousand dollars a year. The owner earns 400 thousand. The job ad says, "Our average salary is $71 thousand!"
import pandas as pd salary = pd.Series([30, 31, 32, 33, 34, 35, 36, 38, 41, 400]) print(salary.mean(), salary.median()) print((salary < salary.mean()).mean())
Output71.0 34.5
0.9The ad is technically correct: the ten salaries add up to 710 thousand, and the mean is 71 thousand. But nine out of ten people are below this mean, and the only person above it is the owner. For someone who applies for a job, the meaningful number is the median: $34.5 thousand (the number of observations is even, and the two middle values are 34 and 35).
If the owner raises their own salary from 400 to 800 thousand dollars, what happens to the mean and the median in the ad?
Which measure fits each question?
For each row, pick its match from the list, then check.
Finally, here is a good question: "Which measure should I use to estimate next month's revenue?" The answer is the mean, not the median that describes the typical order. If you multiply the expected number of orders by the mean amount, you find the total. If you multiply by the median, you systematically forget the revenue that large orders bring. The same data, two different questions, two different correct measures.
Let's compare mobile and desktop orders: the number of orders, the mean, and the median side by side.
import pandas as pd df = pd.read_csv("orders.csv") print(df.groupby("device")["amount"].agg(["count", "mean", "median"]).round(2))
Output count mean median
device
desktop 369 84.90 44.95
mobile 516 68.83 44.90The medians are almost the same ($44.95 and $44.90): the typical order is the same size on both devices. The means differ by about $16. The difference is not in the middle of the distribution but in the upper tail: large orders are more frequent or larger on desktop. It is more accurate to say "The typical order is the same on both devices, and large orders are concentrated on desktop" than to say "Desktop customers spend more."
Let's look at what the three measures say for an ordinal variable.
import pandas as pd s = pd.read_csv("survey.csv") print(s["satisfaction"].mode()[0], s["satisfaction"].median(), round(s["satisfaction"].mean(), 2))
Output4 4.0 3.93The mode and the median are 4: the most frequent score and the score in the middle of the order are both 4. The mean is 3.93. It depends on the assumption that the distances between scores are equal, so it is good to give it together with the mode or the median in a report.
Reporting only the mean for a right-skewed variable. The sentence "The average order is $75.53" hides that half of the orders are $44.95 or less, and that three out of four orders do not reach this "average." With skewed data, give the median too.
The second common mistake is to think that the result of mode() is a single number. The following line prints a Series, not the score:
Outputprint("Most frequent score:", s["satisfaction"].mode())The output also shows the lines 0 4 and Name: satisfaction. If you use the result in a calculation, you also work with a Series. For a single value, write s["satisfaction"].mode()[0].
mode() returns a Series, and mode()[0] gives a single value).Next lesson: You saw the means of categories. But does the plain mean of the five category means give the overall mean? The weighted average is next.
Exercises and quiz are for members
This lesson has 6 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.