Do the last two lessons again with pandas: read the CSV into a table, then take an average, a maximum and a filtered table in one line each.
The last lesson took a loop to find an average. A data scientist writes it in one line.
You have counted, totalled and filtered by hand with loops. pandas is the tool people use for exactly those jobs. It reads a CSV into a table (a DataFrame), keeps numbers as numbers, and gives each job a name: .mean() for the average, .max() for the biggest, len(...) for how many rows.
import pandas as pd loads it, and pd.read_csv("music.csv") reads the file. Copy those two lines for now: you will use them every time.
Make your prediction first. Help opens once you've locked it in.
import pandas as pd
songs = pd.read_csv("music.csv")
print(round(songs["minutes"].mean(), 1))
songs["minutes"] is one column of the table. What does .mean() do to it?
Run it (pandas takes a few seconds to load the first time) and read the output next to the loops you wrote last lesson.
| Line | What it does | The loop it replaces |
|---|---|---|
pd.read_csv("music.csv") |
Reads the file into a table | open, csv.DictReader and list |
songs["minutes"].mean() |
Average of a column | Adding in a loop, then dividing |
songs["minutes"].max() |
Biggest in a column | Keeping the longest so far |
songs[songs["genre"] == "pop"] |
Only the rows where the test is true | A loop with an if |
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"] == "pop"]
print(len(pop), "pop songs, average", round(pop["minutes"].mean(), 1), "minutes")
import pandas as pd
songs = pd.read_csv("music.csv")
rock = songs[songs["genre"] == "rock"]
print(len(rock))
print(rock["title"])
len(table) is the number of rows.
Inside out: the test first, then the keep.
Run the code. How many rock songs are there?
Change the program so the last line is about rock songs instead of pop.
Before a hint: what kind of stuck is this? Try a strategy first
Change the word in the test, and the name the output uses.
Use "rock" in place of "pop".
Show the number of rock songs and their average length.
Run your program. This ticks when your code does all of the above.
Find out how many songs are from before 1990, and how long the longest of them is.
Before a hint: what kind of stuck is this? Try a strategy first
Filter the table with a test on the year column, then use len and .max().
old = songs[songs["year"] < 1990]
Print how many songs are from before 1990 and the length of the longest one.
Run your program. This ticks when your code does all of the above.
Lesson done. You've finished every step you need.
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. About 30 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Reading a filter in one line. Say it aloud: the songs where the genre is pop.
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.
Next lessons: a chart and cleaning by hand, then the dashboard, where pandas sits behind an app.