Filter

Click here to open the slides full screen.

from setup import ___

from siuba import *
from plotnine import *

from music_top200 import music_top200, track_features

Exercise 1:

The code below is filtering to keep only hits where country is United States. Change the filter to get hits from Canada.

(music_top200
  >> filter(_.country == "United States")
)
country position track_name artist streams duration continent
7800 United States 1 The Box Roddy Ricch 12987027 196.653 Americas
7801 United States 2 Myron Lil Uzi Vert 9163134 224.955 Americas
7802 United States 3 Blueberry Faygo Lil Mosey 8043475 162.547 Americas
... ... ... ... ... ... ... ...
7997 United States 198 Lights Up Harry Styles 1606234 172.227 Americas
7998 United States 199 Without Me Halsey 1606153 201.661 Americas
7999 United States 200 Enemies (feat. DaBaby) Post Malone 1597824 196.760 Americas

200 rows × 7 columns

(music_top200
  >> filter(_.country == "Canada")
)
country position track_name artist streams duration continent
1400 Canada 1 The Box Roddy Ricch 1662638 196.653 Americas
1401 Canada 2 Dance Monkey Tones And I 1287622 209.755 Americas
1402 Canada 3 Blinding Lights The Weeknd 1233608 201.573 Americas
... ... ... ... ... ... ... ...
1597 Canada 198 Envy Me Calboy 211725 120.137 Americas
1598 Canada 199 Into the Unknown Idina Menzel 211633 194.848 Americas
1599 Canada 200 Dancing With A Stranger (with Normani) Sam Smith 210840 171.030 Americas

200 rows × 7 columns

Comparing results, which artist is in the top 3 in both (the) United States and Canada?

Roddy Ricch Nailed it!
The Weeknd That artist is only top 3 in the United States
Tones and I That artist is only top 3 in the Canada

Exercise 2:

  • Filter to keep tracks where position equals 5.
(music_top200
  >> filter(___)
)
⚠️: Don't forget to replace all the blanks!
(music_top200
  >> filter(_.position == 5)
)
country position track_name artist streams duration continent
4 Argentina 5 La Difícil Bad Bunny 1205852 163.084 Americas
204 Austria 5 Dance Monkey Tones And I 162646 209.755 Europe
404 Australia 5 The Box Roddy Ricch 1330929 196.653 Oceania
... ... ... ... ... ... ... ...
11804 Uruguay 5 Hola - Remix Dalex 77471 249.520 Americas
12004 Viet Nam 5 Black Swan BTS 109112 198.261 Asia
12204 South Africa 5 Godzilla (feat. Juice WRLD) Eminem 58462 210.800 Africa

62 rows × 7 columns

Which artist is in position 5 in South Africa?

Bad Bunny Incorrect
Eminem That’s right!
BTS Incorrect
Selena Gomes Incorrect

Exercise 3:

Find the top 5 songs in the country Hong Kong.

In the slides we discussed the == operator. Here is a longer list of some options!

operator meaning
== is equal to
< is less than
> is greater than
(music_top200
  >> filter(___, ___)
)
⚠️: Don't forget to replace all the blanks!
(music_top200
  >> filter(_.country == "Hong Kong", _.position < 6)
)
country position track_name artist streams duration continent
4600 Hong Kong 1 WANNABE ITZY 112648 191.242 Asia
4601 Hong Kong 2 Intentions (feat. Quavo) Justin Bieber 104467 212.867 Asia
4602 Hong Kong 3 Señorita Shawn Mendes 84196 190.960 Asia
4603 Hong Kong 4 Don't Start Now Dua Lipa 80457 183.290 Asia
4604 Hong Kong 5 Dance Monkey Tones And I 79376 209.755 Asia

5 rows × 7 columns

Exercise 4: looking at The Weeknd’s streams

How many times has the artist The Weeknd had over 1,000,000 streams?

Hint: Do in steps. Run first to get all rows where the artist is The Weeknd, and then modify your code to get where he has over 1,000,000 streams.
(

)
()
(music_top200
  >> filter(_.artist == "The Weeknd", _.streams > 1000000)
)
country position track_name artist streams duration continent
400 Australia 1 Blinding Lights The Weeknd 1757343 201.573 Oceania
1248 Brazil 49 Blinding Lights The Weeknd 1343243 201.573 Americas
1402 Canada 3 Blinding Lights The Weeknd 1233608 201.573 Americas
... ... ... ... ... ... ... ...
7841 United States 42 After Hours The Weeknd 3672033 361.027 Americas
7906 United States 107 Heartless The Weeknd 2265446 200.080 Americas
8210 Mexico 11 Blinding Lights The Weeknd 3785306 201.573 Americas

13 rows × 7 columns