Show Each Book's Shelf Code
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.
| book_id | title |
|---|---|
| 1 | The Quiet Tide |
| 7 | Mapping Rivers |
| 11 | Garden Almanac |
| 90 | Northern Light |
| 3 | Paper Boats |
| book_id | shelf_code |
|---|---|
| 3 | 1 |
| 11 | 2 |
| 90 | 3 |
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 booksSolve 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