AnalystPath

Show Each Book's Shelf Code

PandasEasyJunior level~10 min

Problem

A community library keeps a catalogue of every book it owns, plus a separate sheet recording the physical shelf code for books already placed on the shelves.

`books` (`books.csv`): `book_id`, `title` — one book per row.

`shelf_placements` (`shelf_placements.csv`): `book_id`, `shelf_code` — one row per shelved book, giving where it was placed. A book that has not yet been shelved has no row here.

Return a DataFrame with `shelf_code` and `title` for every book. If a book has not been placed on a shelf, its `shelf_code` should be null (`NaN`). Rows may be in any order.

Input data

Example rows — the live problem includes the full dataset.

books
book_idtitle
1The Quiet Tide
7Mapping Rivers
11Garden Almanac
90Northern Light
3Paper Boats
shelf_placements
book_idshelf_code
31
112
903

Expected output

Your answer should return 5 rows with the columns shelf_code, title.

Starter code (Pandas (Python))

import pandas as pd

def book_shelf_codes(books: pd.DataFrame, shelf_placements: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return books

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