Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Answering a question from data (pandas)

After this lesson: I can answer a question from a data set.

45 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it meansExample
Data seta collection of records about one thing
Meanthe total divided by how many values there are
Medianthe middle value once the values are in order
Outliera value far away from the rest
Groupthe rows that share a valueevery sunny day
pandasa 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

MeanMedian
Tips in pounds2, 3, 3, 4, 382, 3, 3, 4, 38
Howtotal 50, divided by 5the middle of five values
Answer£10£3
Is it typical?No, one party tipped £38Yes

The cells students fill in are left blank in this preview.

Mean and median by hand

Code: python
# 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

Command
docker exec jupyter python3 steps/summary.py

Shown 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

Command
docker exec jupyter python3 steps/weekend.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Group the days by weather

Command
docker exec jupyter python3 steps/weather.py

Shown 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

Code: python
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.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.