Top-Streamed Track in Each Genre
Problem
A music service stores monthly stream counts per track; each track belongs to a genre.
**DataFrame: `tracks`**
| Column | Type |
|------------|---------|
| track_id | int |
| track_name | varchar |
| streams | int |
| genre_id | int |
`track_id` is unique; `genre_id` references `genres.genre_id`.
**DataFrame: `genres`**
| Column | Type |
|------------|---------|
| genre_id | int |
| genre_name | varchar |
`genre_id` is unique.
For each genre, return the track(s) with the **highest** stream count in that genre. If several tracks tie for the most streams in a genre, return all of them. Return three columns named `genre`, `track`, and `streams`, in any order.
Input data
Example rows — the live problem includes the full dataset.
| track_id | track_name | streams | genre_id |
|---|---|---|---|
| 1 | Dawn Ride | 70000 | 1 |
| 2 | Neon Pulse | 90000 | 1 |
| 3 | Quiet Harbor | 80000 | 2 |
| 4 | Slow Tide | 60000 | 2 |
| 5 | City Lights | 90000 | 1 |
| genre_id | genre_name |
|---|---|
| 1 | Electronic |
| 2 | Ambient |
Expected output
Your answer should return 3 rows with the columns genre, track, streams.
Starter code (Pandas (Python))
import pandas as pd
def top_track_per_genre(tracks, genres) -> pd.DataFrame:
# Your code here
return tracksSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock