Genres Commonly Heard by the Same Listeners
Problem
You are given two DataFrames. `plays` has columns `listener_id`, `track_id`, and `play_count`; each row records that a listener played a track some number of times, and the same (listener, track) can appear more than once. `tracks` has columns `track_id`, `genre`, and `length_sec`, one row per track.
Find every pair of genres for which at least three distinct listeners played a track from **both** genres. Treat each pair once, listing the alphabetically smaller genre first.
Return a DataFrame with columns `genre1`, `genre2`, and `listener_count`, ordered by `listener_count` descending, then `genre1` ascending, then `genre2` ascending.
Input data
Example rows — the live problem includes the full dataset.
| listener_id | track_id | play_count |
|---|---|---|
| 1 | 101 | 2 |
| 1 | 102 | 1 |
| 1 | 201 | 3 |
| 1 | 301 | 1 |
| 2 | 101 | 1 |
| track_id | genre | length_sec |
|---|---|---|
| 101 | Rock | 210.0 |
| 102 | Jazz | 180.0 |
| 103 | Jazz | 240.0 |
| 201 | Pop | 200.0 |
| 202 | Pop | 195.0 |
Expected output
Your answer should return 4 rows with the columns genre1, genre2, listener_count.
Starter code (Pandas (Python))
import pandas as pd
def genres_commonly_heard_by_the_same_listeners(plays, tracks) -> pd.DataFrame:
# Your code here
return playsSolve 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