Turn a list of song lengths into a numpy array, then do sums on every number at once: times 60, an average, and a test that keeps only the long songs.
To change every number in a list, you would write a loop. With numpy you write one line.
A Python list can hold numbers, but it cannot do sums on them all at once. numpy (said "num-pie") is the tool behind pandas, and it gives you the array: a list of numbers you can add up, multiply and test in one step.
import numpy as np loads it, and np.array([...]) turns a list into an array. Copy those for now: you will use them every time.
Make your prediction first. Help opens once you've locked it in.
import numpy as np
lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])
print(lengths * 60)
lengths * 60 has an array on the left. What does it do to each number?
Run it (numpy takes a few seconds to load the first time) and read each line of output.
| Line | What it does | The loop it replaces |
|---|---|---|
lengths * 60 |
Times 60 on every number | A loop that builds a new list |
lengths.mean() |
The average | Adding in a loop, then dividing |
lengths.max() |
The biggest | Keeping the largest so far |
lengths > 4 |
True or False for every number | A loop with an if |
lengths[lengths > 4] |
Only the numbers where the test is True | A loop that keeps some |
import numpy as np
lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])
print("Lengths:", lengths)
print("In seconds:", lengths * 60)
print("Average:", round(lengths.mean(), 2), "minutes")
print("Longest:", lengths.max(), "minutes")
print(lengths > 4)
long_songs = lengths[lengths > 4]
print(long_songs)
import numpy as np
lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])
long_songs = lengths[lengths > 4]
print(len(long_songs))
print(long_songs)
len(array) is how many numbers are in it.
Inside out: the test first, then the keep.
Run the code. How many songs are longer than 4 minutes?
Change the program so it prints the average length in seconds as well as minutes.
Before a hint: what kind of stuck is this? Try a strategy first
Times every length by 60 first, then take the mean.
(lengths * 60).mean()
Show the average length in seconds.
Run your program. This ticks when your code does all of the above.
Find out how many songs are shorter than 4 minutes, and the average length of just those songs.
Before a hint: what kind of stuck is this? Try a strategy first
Make a short array with a test on lengths, then use len and .mean().
short = lengths[lengths < 4]
Print how many songs are under 4 minutes and their average length.
Run your program. This ticks when your code does all of the above.
Lesson done. You've finished every step you need.
An optional extension after pandas: students make a numpy array from a list, multiply or average it in one line, and use a test inside square brackets to keep some numbers. About 25 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Reading a filter in one line. Say it aloud: the lengths where the length is over 4.
Stretch: Print the lengths in seconds as whole numbers with np.round(lengths * 60).
Support: Give the whole program with the 4 blank for students to fill in.
Pandas uses numpy underneath, so the same square-bracket test you used on a table works on an array too.