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

Free lesson

JupyterLab Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab10 min read7 commands

Will it be busy tomorrow?

A bike hire shop has to decide how many bikes to put out each morning. Too few and people walk away. Too many and they sit in the rain. A year of records can answer the question better than a guess, if you can read them.

By the end of this lesson

Note

I can use pandas to load a table of data and summarise it.

Key words

  • Data set: a collection of records about one thing.
  • CSV file: a table saved as text, one row a line, commas between the values.
  • DataFrame: a table in pandas, with named columns.
  • Mean: the total divided by how many there are.
  • Notebook: a page of code, results and words, run one cell at a time.
  • Model: a rule worked out from data that predicts a value.

Starter: what you already know

Note

Answer in your head. 1. What does a row of a table hold? 2. How do you work out the mean of 2, 4 and 6? 3. What does print do in Python? You will use all three in this lesson. Answers: one record; add them and divide by 3, which is 4; it shows a value on the screen.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. There is one container, jupyter, and the terminal reaches its Python with docker exec jupyter, which runs one command inside it. The JupyterLab link opens its web page, port 8888. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

A data project is a folder of data and code

ls lists the work folder: the data hires.csv, the notebook start.ipynb and the folder steps. cat prints the first script. It is four lines of Python: bring in pandas, read the file, and print its size and its first rows.

Command
docker exec jupyter ls

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

Command
docker exec jupyter cat steps/load.py

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

pandas reads a CSV file into a DataFrame

pd.read_csv reads the whole file into a DataFrame called hires. The answer starts (365, 7): 365 rows, one a day, and 7 columns. Then come the first five rows. The first is 1 January 2025, a cloudy Wednesday at 0.9 degrees, with 137 bikes hired.

Command
docker exec jupyter python3 steps/load.py

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

A summary describes a whole column

describe sums up one column in eight numbers. The hires column has a mean of 270.3 bikes a day, a quietest day of 41 and a busiest of 487. The second script uses groupby to split the rows by weather and take the mean of each group: sunny 312.2, cloudy 267.0 and rainy 227.6.

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.

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.

Check

Tip

Which column did groupby split the rows by, and which weather had the most hires? Answer: the weather column, and sunny days, with a mean of 312.2.

A picture shows a pattern a table hides

Matplotlib draws charts. This script draws one dot for every day, the temperature across and the bikes hired up, and saves it as hires.png. Open it from the file list in JupyterLab: the dots climb from left to right, so warmer days are busier.

Command
docker exec jupyter python3 steps/picture.py

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

A model turns the pattern into a rule

scikit-learn fits a linear regression: a straight-line rule from temperature and rain to hires. The answer says one more degree is worth about 11.2 hires, and one more millimetre of rain costs about 8.2. R squared is 0.776, so the rule explains about three quarters of the change from day to day.

Command
docker exec jupyter python3 steps/model.py

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

A common mistake

Warning

Many people think a model that fits well shows what causes the change. It shows only that the values move together in this data. Warm days are busier, but they are also drier and more often at the weekend in summer. Say "goes with", not "causes", unless something else proves it.

The same work in a notebook

Open JupyterLab from the bar and double-click start.ipynb. It does what the five scripts did, one cell at a time. Click a cell and press Shift and Enter to run it. The answer appears under the cell, and the chart appears in the page. The last cell is where your own question goes.

Exit check

Tip

Answer without looking back. 1. Which pandas function reads a CSV file? 2. How many rows does hires.csv have, and why that number? 3. Which method splits the rows into groups? 4. Does a good model prove what causes the change? Answers: pd.read_csv; 365, one for each day of 2025; groupby; no, it shows the values move together.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service, JupyterLab with pandas, NumPy, Matplotlib and scikit-learn, and nothing here marks itself: the student hands in a notebook of their own over hires.csv, downloaded from JupyterLab, and a sentence on what it showed. Key learning points: 1. pandas reads a CSV file into a DataFrame. 2. describe and groupby summarise a column. 3. A chart and a model show a pattern. Common mistake: reading a good fit as a cause. Equipment: the lab and a browser. Tip: ask students to guess each number before they press Run. The data is made up from a fixed seed, so every copy has the same numbers. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

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

Teaching a class? Set these lessons for your students with marking and a lab for every student.