AnalystPath

Leaderboard Shake-Up

PandasMediumMid level~10 min

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.

coffee_shops
shop_idshop_namerevenue
3Maple1400
1Harbor2100
2Birch1390
4Cedar1800
revenue_adjustments
shop_idrevenue_delta
3600
20
410
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_shops

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