Library Checkouts by Weekday and Genre
Problem
A public library tracks how many copies of each title were checked out.
You are given two DataFrames:
`titles` (one row per title)
- `title_id` - title identifier
- `title_name` - title name
- `genre` - the genre of the title
`checkouts` (one row per checkout)
- `checkout_id` - checkout identifier
- `patron_id` - the patron who checked the title out
- `taken_on` - the date of the checkout
- `title_id` - the title that was taken
- `copies` - how many copies were taken in that checkout
For every genre, report the total number of copies checked out on each day of the week. Return one row per genre with columns `genre`, `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday`, `Sunday`. A genre with no checkouts on a given day must show 0 for that day, and every genre in the catalog must appear even if it had no checkouts at all. Order the result by `genre`.
Input data
Example rows — the live problem includes the full dataset.
| title_id | title_name | genre |
|---|---|---|
| f1 | Tide Lines | Fiction |
| f2 | Glass Harbor | Fiction |
| c1 | Star Patrol | Comic |
| c2 | Night Vault | Comic |
| m1 | Engine Care | Manual |
| checkout_id | patron_id | taken_on | title_id | copies |
|---|---|---|---|---|
| 1 | 1 | 2022-06-01 | f1 | 10 |
| 2 | 2 | 2022-06-01 | f2 | 10 |
| 3 | 3 | 2022-06-02 | f1 | 5 |
| 4 | 4 | 2022-06-03 | c1 | 5 |
| 5 | 5 | 2022-06-05 | c2 | 7 |
Expected output
Your answer should return 4 rows with the columns genre, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
Starter code (Pandas (Python))
import pandas as pd
def library_checkouts_by_weekday_and_genre(titles, checkouts) -> pd.DataFrame:
# Your code here
return titlesSolve 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