Are weekends twice as busy?
You work Saturdays at a bike hire shop. The owner says weekends are twice as busy as weekdays, so she wants two more staff.
Is she right? She has a year of records. With the right question and two numbers, you can check in a minute.
By the end of this lesson
Note
I can answer a question from a data set.
Key words
| Word | What it means | Example |
|---|---|---|
| Data set | a collection of records about one thing | |
| Mean | the total divided by how many values there are | |
| Median | the middle value once the values are in order | |
| Outlier | a value far away from the rest | |
| Group | the rows that share a value | every sunny day |
| pandas | a Python library for working with tables |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top and wait until it is running. Each Run starts a short Python script inside the lab. Later you will type code into a notebook: open JupyterLab from the bar for that.
Mean and median both mean typical
The mean adds every value and divides by how many there are. The median puts the values in order and takes the middle one. With an even number of values, it is halfway between the two in the middle.
Often the two are close. When one value is far from the rest, they are not.
Worked example: a week of tips
| Mean | Median | |
|---|---|---|
| Tips in pounds | 2, 3, 3, 4, 38 | 2, 3, 3, 4, 38 |
| How | total 50, divided by 5 | the middle of five values |
| Answer | £10 | £3 |
| Is it typical? | No, one party tipped £38 | Yes |
The cells students fill in are left blank in this preview.
Mean and median by hand
# Bikes hired on each day of one week.
week = [212, 173, 327, 204, 160, 188, 455]
total = 0
for one in week:
total = total + one
print("mean:", round(total / len(week), 1))
# The median is the middle value once they are in order.
in_order = sorted(week)
print("median:", in_order[len(in_order) // 2])
# Change 455 to 1000 and press Run again.
# Which answer moves, and which stays the same?
Summarise a year of hires
docker exec jupyter python3 steps/summary.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Reading the summary
count is 365 days. The mean is 270.3 bikes a day. The row 50% is the median: 273. The quietest day had 41 and the busiest 487.
The rows 25% and 75% are quarters: a quarter of the days had 216 or fewer, and a quarter had 327 or more. The mean and the median are close, so no outlier is pulling the mean far.
A common mistake
Warning
Many people think the mean is always the typical value. It is only typical when no value sits far from the rest. So look at the median too. If the two are far apart, an outlier is pulling the mean, and the median is the fairer answer.
Split the rows into groups to compare
To answer "are weekends busier?", split the days into two groups, weekdays and weekend days. Then work out the mean of each group.
pandas does this with groupby. You say which column decides the groups, then which column to sum up. The script below adds a column that is True on a Saturday or a Sunday.
Compare weekdays with weekends
docker exec jupyter python3 steps/weekend.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Group the days by weather
docker exec jupyter python3 steps/weather.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
In your notebook: the median for each weather
import pandas as pd
hires = pd.read_csv("hires.csv")
# The middle day for each kind of weather.
hires.groupby("weather")["hires"].median()
Going further: the quietest month
In your notebook, change "weather" to "month" in the last cell and run it again. Which month has the lowest median, and which the highest?
Then write one sentence for the shop owner that answers a question of your own. Give the number and say whether you used the mean or the median.