Each Reader's Favourite Genre
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.
| reader_id | display_name |
|---|---|
| 1 | Nora |
| 2 | Sven |
| 3 | Yuki |
| 4 | Diego |
| 5 | Amara |
| read_id | read_on | reader_id | genre_id |
|---|---|---|---|
| 1 | 2022-07-31 | 1 | 11 |
| 2 | 2022-07-30 | 2 | 12 |
| 3 | 2022-08-29 | 3 | 13 |
| 4 | 2022-07-29 | 4 | 11 |
| 5 | 2022-06-10 | 1 | 12 |
| genre_id | genre_name |
|---|---|
| 11 | Mystery |
| 12 | Romance |
| 13 | SciFi |
| 14 | History |
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 readersSolve 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