[Coding Question] “Top 10 Songs of 2010” – Spotify

Photo from Vishnu R Nair on Pexels

Photo from Vishnu R Nair on Pexels

This coding question deals with finding the top values in a given column based on several criteria, and also involves sorting the data and limiting the number of columns that are going to be returned.

You can find the coding question here.

Here you will find the solution:

df = billboard_top_100_year_end

# filter based on year and year_rank
data = df[(df.year == 2010) & (df.year_rank <= 10)]

# removed duplicates
data = data.drop_duplicates('year_rank')

# sort the data
data.sort_values(by = 'year_rank')

# the final answer
data[['year_rank', 'group_name', 'song_name']]

# one liner
df[(df.year == 2010) & (df.year_rank <= 10)].drop_duplicates('year_rank').sort_values(by = 'year_rank')[['year_rank', 'group_name', 'song_name']]
# one liner
df[(df.year == 2010) & (df.year_rank <= 10)].drop_duplicates('year_rank').sort_values(by = 'year_rank')[['year_rank', 'group_name', 'song_name']]

 

You can find a complete explanation and walkthrough of the problem here:

 

 

Related Images: