AnalystPath

Library Checkouts by Weekday and Genre

PandasHardSenior level~10 min

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.

titles
title_idtitle_namegenre
f1Tide LinesFiction
f2Glass HarborFiction
c1Star PatrolComic
c2Night VaultComic
m1Engine CareManual
checkouts
checkout_idpatron_idtaken_ontitle_idcopies
112022-06-01f110
222022-06-01f210
332022-06-02f15
442022-06-03c15
552022-06-05c27

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 titles

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