AnalystPath

Listener Engagement Profile on a Music Service

PandasMediumMid level~10 min

Problem

You are given `plays` (from `plays.csv`: `play_id`, `listener_id`, `track_id`, `played_on` date, `seconds_listened` float) and `tracks` (from `tracks.csv`: `track_id`, `genre`, `duration` float). Inner-join plays to tracks on `track_id`, then for each listener compute: `total_seconds` = round(sum of seconds_listened, 2); `play_count` = number of plays; `unique_genres` = count of distinct genres; `avg_seconds` = round(mean seconds_listened, 2); `top_genre` = the genre the listener played most often, breaking ties by the most recent `played_on`; and `engagement_score` = round(play_count*10 + total_seconds/100, 2). Return columns `listener_id`, `total_seconds`, `play_count`, `unique_genres`, `avg_seconds`, `top_genre`, `engagement_score`, sorted by `engagement_score` descending then `listener_id` ascending.

Input data

Example rows — the live problem includes the full dataset.

tracks
track_idgenreduration
1Jazz240.00
2Jazz200.00
3Techno300.00
4Folk180.00
plays
play_idlistener_idtrack_idplayed_onseconds_listened
150012024-01-05120.00
250022024-01-08200.00
350032024-02-01300.00
460032024-01-10100.00
560042024-03-15150.00

Expected output

Your answer should return 2 rows with the columns listener_id, total_seconds, play_count, unique_genres, avg_seconds, top_genre, engagement_score.

Starter code (Pandas (Python))

import pandas as pd

def listener_profile(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