← Teacher guide

The same job in pandas

Students redo the CSV jobs with pandas: read_csv into a table, .mean() and .max() on a column, len for rows, and a test inside square brackets to filter.

Data detective · Reading data · about 30 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 8 min · Modify and Make 12 min

Running the lesson

  1. Predict: the error option catches students who remember float(). pandas reads numbers as numbers.
  2. Run: put last lesson's loop beside each pandas line. The point is that pandas is the same job with a name.
  3. Investigate: read the filter from the inside out. The test makes True and False for every row.
  4. Make: the test uses a number, so no quotes round 1990.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Reading a filter in one line. Say it aloud: the songs where the genre is pop.

When students get stuck

What you will seeA question to askThe fix
KeyError: 'minute'What is the column called in the file?Column names must match the first line of the CSV exactly, and the same capitals.
A table printed instead of a number.Did you pick a column before .mean()?songs["minutes"].mean() picks the column first.
The filter test uses a single =.Is this a test or an assignment?A test needs ==. A single = stores a value.

Answers to the checks

  1. Predict: The file has 49 songs. What does the last line print?
    About 3.9. songs["minutes"] is the whole minutes column, and .mean() averages it. pandas read the numbers as numbers, so there is no float() step.
  2. Count the rock songs: Run the code. How many rock songs are there?
    10
  3. Read the filter: What does `songs[songs["genre"] == "rock"]` give?
    A smaller table with only the rock rows
    Inside the brackets the test is True or False for each row, and the outer brackets keep the True rows. It is the loop and if from last lesson, in one line.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

import pandas as pd

songs = pd.read_csv("music.csv")

print(songs.head())
print(len(songs), "songs")
print("Average length:", round(songs["minutes"].mean(), 1), "minutes")
print("Longest:", songs["minutes"].max(), "minutes")

pop = songs[songs["genre"] == "rock"]
print(len(pop), "pop songs, average", round(pop["minutes"].mean(), 1), "minutes")

Make

import pandas as pd

songs = pd.read_csv("music.csv")

old = songs[songs["year"] < 1990]
print(len(old), "songs before 1990")
print("Longest:", old["minutes"].max(), "minutes")

Stretch and support

Stretch: Print the average length of every genre with songs.groupby("genre")["minutes"].mean().

Support: Give the whole program with the genre blank for students to fill in.

Exit ticket

What does songs[songs["year"] < 1990] do?

It keeps only the rows where the year is below 1990, and gives them back as a smaller table.

What each step is for

Where it leads

Next lessons: a chart and cleaning by hand, then the dashboard, where pandas sits behind an app.