Spotify's weekly chart is in charts: one row per track per week, with its chart position. Find how many weeks each track spent at number one. Return a DataFrame result with the columns track and weeks, most weeks first, and tracks with the same number of weeks in alphabetical order. A track that never reached number one is left out.
The data
charts32 rows
| weekint64 | positionint64 | trackobject | artistobject |
|---|
| 1 | 1 | Neon Rain | Juno Vale |
| 1 | 2 | Paper Moons | The Ferrymen |
| 1 | 3 | Low Tide | Ama Reyes |
| 1 | 4 | Glass Houses | Kit Marlowe |
| 2 | 1 | Neon Rain | Juno Vale |
| 2 | 2 | Glass Houses | Kit Marlowe |
and 26 more rows
Target output
| track | weeks |
|---|
| Neon Rain | 3 |
| Paper Moons | 3 |
| Glass Houses | 1 |
| Low Tide | 1 |
What this question tests
Tests the core pandas loop: keep the rows that matter, count per group, then sort on two keys in two directions. It is the warm-up of many analyst screens because it shows in a minute whether you filter before you aggregate, and whether you know that a tie needs a rule.
More Python interview questions