Each Reader's Three Latest Loans
Problem
A library logs every book loan.
`readers` columns: `reader_id`, `reader_name`.
`loans` columns: `loan_id`, `loaned_on`, `reader_id`, `fine`.
For each reader, report their **three most recent loans** (by loan date, latest first). If a reader has fewer than three loans, return all of them.
Return `reader_name`, `reader_id`, `loan_id`, and `loaned_on`. Order the result by `reader_name`, then `reader_id`, then `loaned_on` descending.
Input data
Example rows — the live problem includes the full dataset.
| reader_id | reader_name |
|---|---|
| 1 | Wendell |
| 2 | Bianca |
| 3 | Soraya |
| 4 | Mateo |
| 5 | Kelyn |
| loan_id | loaned_on | reader_id | fine |
|---|---|---|---|
| 1 | 2021-07-31 | 1 | 30 |
| 2 | 2021-07-30 | 2 | 40 |
| 3 | 2021-07-31 | 3 | 70 |
| 4 | 2021-07-29 | 4 | 100 |
| 5 | 2021-06-10 | 1 | 1010 |
Expected output
Your answer should return 9 rows with the columns reader_name, reader_id, loan_id, loaned_on.
Starter code (Pandas (Python))
import pandas as pd
def each_readers_three_latest_loans(readers, loans) -> pd.DataFrame:
# Your code here
return readersSolve 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