Top Three Food Trucks Per City
Problem
You are given a DataFrame `trucksales` with columns `sale_id`, `city`, `revenue`, and `truck_name`. Each row is one sale a food truck made at a stop.
For every `city`, find the three food trucks with the highest **total revenue**. Break ties by `truck_name` alphabetically (A first). Output four columns: `city`, `first_truck`, `second_truck`, `third_truck`. Each truck cell must read as `truck_name (total_revenue)` (for example `TacoTide (320)`). If a city has fewer than three trucks, put the text `No runner up` for a missing second place and `No third place` for a missing third place. Order the result by `city` ascending.
Input data
Example rows — the live problem includes the full dataset.
| sale_id | city | revenue | truck_name |
|---|---|---|---|
| 1 | Austin | 120 | TacoTide |
| 2 | Austin | 200 | TacoTide |
| 3 | Austin | 150 | BunBros |
| 4 | Austin | 90 | CurryCart |
| 5 | Denver | 300 | WaffleWay |
Expected output
Your answer should return 3 rows with the columns city, first_truck, second_truck, third_truck.
Starter code (Pandas (Python))
import pandas as pd
def top_three_trucks_per_city(trucksales) -> pd.DataFrame:
# Your code here
return trucksalesSolve 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