Lookup Reservoir Levels
Problem
A water authority stores the recorded level of each reservoir at the end of selected years. Analysts submit lookup requests for a reservoir in a given year; if no level was recorded for that reservoir-year, the answer is `0`.
DataFrame `readings` (from `readings.csv`) has columns `reservoir_id`, `year`, `level_cm` — the recorded end-of-year level in cm for a reservoir.
DataFrame `lookups` (from `lookups.csv`) has columns `reservoir_id`, `year` — one request per row.
For every lookup, return `reservoir_id`, `year`, and `level_cm`. If the reservoir-year is missing from `readings`, report its level as `0`. Rows may be returned in any order.
Input data
Example rows — the live problem includes the full dataset.
| reservoir_id | year | level_cm |
|---|---|---|
| 1 | 2018 | 100 |
| 7 | 2020 | 30 |
| 13 | 2019 | 40 |
| 1 | 2019 | 113 |
| 2 | 2008 | 121 |
| reservoir_id | year |
|---|---|
| 1 | 2019 |
| 2 | 2008 |
| 3 | 2009 |
| 7 | 2018 |
| 7 | 2019 |
Expected output
Your answer should return 7 rows with the columns reservoir_id, year, level_cm.
Starter code (Pandas (Python))
import pandas as pd
def resolve_lookups(readings, lookups) -> pd.DataFrame:
# Your code here
return readingsSolve 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