Arrange

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:

Modify the code below to arrange by artist name in descending order.

hint

You can sort something in descending order, using the - operator.

another hint

Start by using the pipe operator with arrange(). You will need to specify what to arrange by.

(music_top200

)
country position track_name artist streams duration continent
0 Argentina 1 Tusa KAROL G 1858666 200.960 Americas
1 Argentina 2 Tattoo Rauw Alejandro 1344382 202.887 Americas
2 Argentina 3 Hola - Remix Dalex 1330011 249.520 Americas
... ... ... ... ... ... ... ...
12397 South Africa 198 Black And White Niall Horan 11771 193.090 Africa
12398 South Africa 199 When I See U Fantasia 11752 217.347 Africa
12399 South Africa 200 Psycho! MASN 11743 197.217 Africa

12400 rows × 7 columns

(music_top200
  >> arrange(-_.artist)
)
country position track_name artist streams duration continent
11783 Taiwan, Province of China 184 一天 (電視劇《想見你》插曲) 黃宣 41615 248.630 Asia
4797 Hong Kong 198 Without You 高爾宣 OSN 19956 175.500 Asia
10745 Singapore 146 Without You 高爾宣 OSN 57383 175.500 Asia
... ... ... ... ... ... ... ...
9737 Philippines 138 This I Promise You *NSYNC 196170 283.507 Asia
11394 Thailand 195 LION (G)I-DLE 44329 210.973 Asia
7153 Latvia 154 GOSHA $NOT 5849 120.000 Europe

12400 rows × 7 columns

What artist is the last observation (row) in the result?

$NOT Great job!
黃宣 That’s not right. Did you arrange in ascending, rather than descending order?
Aaron Smith Incorrect
Y2K Incorrect

Exercise 2:

What is the first track, if you filter to keep only observations from the country Mexico, and then sort in ascending order by track name?

(music_top200
  >> ___
  >> arrange(___)
)
⚠️: Don't forget to replace all the blanks!
(music_top200
  >> filter(_.country == "Mexico")
  >> arrange(_.track_name)
)
country position track_name artist streams duration continent
8263 Mexico 64 11 PM Maluma 1160043 175.733 Americas
8233 Mexico 34 25/8 Bad Bunny 1781488 243.276 Americas
8356 Mexico 157 7 rings Ariana Grande 660372 178.640 Americas
... ... ... ... ... ... ... ...
8291 Mexico 92 bad guy Billie Eilish 958347 194.088 Americas
8329 Mexico 130 death bed (coffee for your head) (feat. beabadoobee) Powfu 765591 173.333 Americas
8337 Mexico 138 everything i wanted Billie Eilish 721895 245.426 Americas

200 rows × 7 columns

11 PM Great job!
一天 (電視劇《想見你》插曲) Incorrect
everything i wanted Incorrect
#24 Be sure to filter where country is Mexico

Exercise 3:

Below is code with the arrange verb removed. Modify it to arrange in ascending order..

  • first by position
  • second by streams
(music_top200
  >> ___
)
⚠️: Don't forget to replace all the blanks!
(music_top200
  >> arrange(_.position, _.streams)
)
country position track_name artist streams duration continent
8000 Cyprus 1 The Box Roddy Ricch 13604 196.653 Asia
7200 Malta 1 Blinding Lights The Weeknd 21416 201.573 Europe
6800 Luxembourg 1 Blinding Lights The Weeknd 22509 201.573 Europe
... ... ... ... ... ... ... ...
1399 Brazil 200 Procuro Alguém (prod. Coyote Beatz) Djonga 542021 213.983 Americas
8399 Mexico 200 Verte Ir DJ Luian 558925 267.500 Americas
7999 United States 200 Enemies (feat. DaBaby) Post Malone 1597824 196.760 Americas

12400 rows × 7 columns

What country has the position 1 track (The Box) with fewest streams?

Cyprus That’s right! Make sure to double check that the track is named The Box by Roddy Ricch.
United States Incorrect
Brazil Incorrect
Luxembourg Incorrect

Exercise 4

What’s the shortest song in the top position in the music_top200 data?

# add code below this line
# NOTE: there are multiple ways to get the answer!
(music_top200
  >> arrange(_.position, _.duration)
)
country position track_name artist streams duration continent
2600 Germany 1 Bad Girls, Good Vibes Ufo361 4081472 107.446 Europe
4400 Greece 1 Drip Snik 367041 147.429 Europe
2000 Colombia 1 Rojo J Balvin 997830 150.720 Americas
... ... ... ... ... ... ... ...
11999 Uruguay 200 Creo Callejeros 11300 289.760 Americas
3999 Finland 200 Without Me Eminem 40460 290.320 Europe
4399 United Kingdom 200 Africa TOTO 334637 295.893 Europe

12400 rows × 7 columns

Bad Girls, Good Vibes That’s right!
Klop Klop Incorrect. This is the shortest track in any position. Can you sort to get top position tracks first?
Susamam Incorrect. This is the longest track in any position. Can you sort to get top position tracks first?
Blockparty Incorrect. This is the shortest track in the last position.