Leaderboard Shake-Up
Problem
You are given two DataFrames. `coffee_shops` has columns `shop_id`, `shop_name`, and `revenue` (`shop_id` is unique). `revenue_adjustments` has columns `shop_id` and `revenue_delta` (`shop_id` is unique, one row per shop).
A shop's rank is by `revenue` descending, breaking ties by `shop_name` ascending (ties share the lowest rank, RANK-style). After adding each shop's `revenue_delta` to its `revenue`, the ranking is recomputed the same way. Return `shop_id`, `shop_name`, and `rank_shift` = old rank minus new rank (positive means the shop climbed, negative means it fell). Row order does not matter.
Input data
Example rows — the live problem includes the full dataset.
| shop_id | shop_name | revenue |
|---|---|---|
| 3 | Maple | 1400 |
| 1 | Harbor | 2100 |
| 2 | Birch | 1390 |
| 4 | Cedar | 1800 |
| shop_id | revenue_delta |
|---|---|
| 3 | 600 |
| 2 | 0 |
| 4 | 10 |
| 1 | -20 |
Expected output
Your answer should return 4 rows with the columns shop_id, shop_name, rank_shift.
Starter code (Pandas (Python))
import pandas as pd
def leaderboard_shake_up(coffee_shops, revenue_adjustments) -> pd.DataFrame:
# Your code here
return coffee_shopsSolve 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