← Teacher guides

Teacher guide: Data detective

A real data set in Python: read a CSV, count, average and find the biggest, draw a bar chart, clean messy data and check it with test cases, then build a dashboard.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Reading data

Read a CSV file into a list of dictionaries, then count, filter, total and average it.

Reading data in 5 minutes (read before teaching this unit)

The idea in plain words

A CSV file is a table saved as plain text: the first line names the columns, every other line is a row, and commas separate the values. Python's csv.DictReader reads it into a list of dictionaries, one per row, so songs[0]["title"] is the first song's title. Every value comes in as text: "2013", not 2013. int() and float() turn it into a number before you compare or add.

Questions about data are loops. Pick rows with a loop and an if. Count them with a variable that goes up by one. Add up a column with a total that starts at 0. Divide by the count for an average. Keep the biggest so far: start with the first row, and replace it only when a row beats it.

Why programmers use them

  • CSV is everywhere. Spreadsheets, school systems, sports sites and government data all export it.
  • The loop answers any question, not only the ones a spreadsheet has a button for: the longest pop song after 2010 is one more if.

An everyday picture, and where it breaks

A stack of index cards, one per song, each with the same labelled boxes. Reading the data is flipping through the stack; a total is a running sum on a scrap of paper. It breaks because the cards say "3.9" in ink: Python treats it as writing, not a number, until you convert it.

Try it yourself (5 minutes)

  1. Open music.csv in the playground's Files tab and find the first song.
  2. Run Lesson 1's program, then change "rock" to "K-pop".
  3. Run Lesson 2's program and check the longest song against the file.

Your own data

music.csv is 49 well-known songs compiled for these lessons (lengths are rounded). Any CSV with a name, a category, a year and a number works with the same programs once the keys change: a class survey, sports results, a Wikidata export of films or games your students like. Students add a file with Add a file from this computer in the playground's Files tab.

Questions students ask

  • Why is "2013" + "1" equal to "20131"? Both are text, and + joins text.
  • Why does longest start as songs[0]? It holds a whole song, so it starts as a real one, ready to compare.
  • Where does the file live? In the browser, for this course (the Files tab). In Python on a computer it would sit in the program's folder.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Charts and checking

Draw a bar chart from counts, then clean messy data and check your code with test cases.

Charts and checking in 5 minutes (read before teaching this unit)

The idea in plain words

Counting with a dictionary. One key per value (each genre), and its count as the value. For each row: if the key is there, add 1; otherwise start it at 1. A second loop over the dictionary draws a bar for each key: "#" * count repeats the character that many times.

Cleaning. Real data has blanks, typos, numbers in words and impossible values. A small cleaning function takes one messy value and gives back a good value, or None ("no value") when it cannot be used. The loop skips the None rows, and the result says how many answers it used.

Test cases. assert clean_hours("400") is None is a test: silent when true, and the program stops with AssertionError when false. A few tests prove the function works, and catch it the moment someone breaks it.

Why programmers use them

  • Charts make patterns visible that a list of numbers hides.
  • Most data work is cleaning, and decisions made from dirty data are wrong.
  • Tests are how programmers trust code: apps you use run thousands of them before every update.

An everyday picture, and where it breaks

Counting is tally marks on a whiteboard, one row per genre; the bar chart is the tally marks themselves. Cleaning is a teacher reading survey slips: "Pop", "pop " and "POP" are the same answer, and "400 hours" goes in the bin. It breaks because a computer never guesses: every rule a human applies without thinking must be written down, and tested.

Try it yourself (5 minutes)

  1. Run Lesson 3's chart, then add print(counts) after the first loop.
  2. Open survey.csv in the Files tab and spot four kinds of mess.
  3. In Lesson 4, change 168 to 16, run, and read the AssertionError. Put it back.

Questions students ask

  • Why not just turn bad answers into 0? It would change the average. Leaving them out, and saying so, is honest.
  • Why is None and not == None? Both work here; is is the usual Python way to check for None.
  • Does the chart tell us what music is popular? No: it shows the songs someone put in the file. Data is a choice.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Dashboards and live data

Put the data behind a small app, then ask a real API for live data (optional).

A dashboard in 5 minutes (read before teaching this unit)

The idea in plain words

pandas reads a CSV into a table (a DataFrame) in one line, pd.read_csv("music.csv"), and keeps numbers as numbers. songs[songs["genre"] == "pop"] keeps the rows where the test is true: the loop and if from Lesson 1 in one line.

Gradio turns a function into an app. gr.Interface(fn, inputs, outputs) draws an input for each item in inputs and an output for each in outputs; pressing Submit calls the function with the inputs' values, in order, and shows what it returns, in order. A dashboard is a function that filters the table, returning a table and a chart.

The playground runs a small stand-in for Gradio so it works in the browser. The same file runs unchanged in real Gradio (pip install gradio pandas), and a teacher can host it as a Hugging Face Space (accounts need age 13+).

Why programmers use them

  • pandas is the standard Python tool for tables of data, from school projects to scientific research.
  • Gradio is a quick way to share a data tool or a machine-learning model with people who do not code.

An everyday picture, and where it breaks

A vending machine: buttons are the inputs, the machinery inside is the function, the tray is the output. It breaks because a Gradio function can be changed in a minute, and the same machine can serve any data.

Try it yourself (5 minutes)

  1. Run Lesson 5's dashboard and choose two genres.
  2. Add the year slider: one item in inputs, one parameter in the function.
  3. Point a classmate at your dashboard and ask them a question it answers.

Questions students ask

  • Why did we do it by hand first? So you know what pandas and Gradio do for you, and can still answer a question they have no button for.
  • Is this a real website? In the playground it runs only in your browser. In real Gradio it is a small web server.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown