AnalystPath

Genres Commonly Heard by the Same Listeners

PandasHardSenior level~10 min

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.

plays
listener_idtrack_idplay_count
11012
11021
12013
13011
21011
tracks
track_idgenrelength_sec
101Rock210.0
102Jazz180.0
103Jazz240.0
201Pop200.0
202Pop195.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 plays

Solve 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

Related Pandas questions