AnalystPath

Each Reader's Favourite Genre

PandasMediumMid level~10 min

Problem

You are given three DataFrames:
- `readers` with `reader_id`, `display_name`
- `reads` with `read_id`, `read_on`, `reader_id`, `genre_id`
- `genres` with `genre_id`, `genre_name`

For each reader, find the genre(s) they read most often (the highest number of `reads` rows). If a reader is tied between several genres at their top count, return every tied genre.

Return columns `reader_id`, `genre_id`, `genre_name`. Readers with no reads do not appear.

Input data

Example rows — the live problem includes the full dataset.

readers
reader_iddisplay_name
1Nora
2Sven
3Yuki
4Diego
5Amara
reads
read_idread_onreader_idgenre_id
12022-07-31111
22022-07-30212
32022-08-29313
42022-07-29411
52022-06-10112
genres
genre_idgenre_name
11Mystery
12Romance
13SciFi
14History

Expected output

Your answer should return 6 rows with the columns reader_id, genre_id, genre_name.

Starter code (Pandas (Python))

import pandas as pd

def each_readers_favourite_genre(readers, reads, genres) -> pd.DataFrame:
    # Your code here
    return readers

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