AnalystPath

Top Three Food Trucks Per City

PandasHardSenior level~10 min

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.

trucksales
sale_idcityrevenuetruck_name
1Austin120TacoTide
2Austin200TacoTide
3Austin150BunBros
4Austin90CurryCart
5Denver300WaffleWay

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 trucksales

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