AnalystPath

Top Earning Market Stall

PandasEasyJunior level~10 min

Problem

DataFrame: `stall` (`stall.csv`)

```text
+------------+--------+
| Column | Type |
+------------+--------+
| stall_id | int |
| produce | object |
| list_cents | int |
+------------+--------+
stall_id uniquely identifies each stall.
```

DataFrame: `purchase` (`purchase.csv`)

```text
+-----------+----------+------------+---------+-------+---------+
| Column | Type |
+-----------+----------+
| vendor_id | int |
| stall_id | int |
| shopper_id| int |
| sold_on | object |
| units | int |
| revenue | int |
+-----------+----------+
Each row records one sale made by a vendor.
```

Write a function that returns the `vendor_id` of the vendor(s) with the **highest total revenue** across all their purchases. If several vendors tie for the top total, return all of them. The `stall` table is not needed for the answer.

Return the rows in any order.

**Example**

Input — `purchase`:

```text
vendor_id stall_id shopper_id sold_on units revenue
1 1 1 2021-01-21 2 2000
1 2 2 2021-02-17 1 800
2 2 3 2021-06-02 1 800
3 3 4 2021-05-13 2 2800
```

Output:

```text
vendor_id
1
3
```

Vendor 1 earned 2000 + 800 = 2800, vendor 3 earned 2800 — they tie for the top, so both are returned; vendor 2 earned only 800.

Input data

Example rows — the live problem includes the full dataset.

stall
stall_idproducelist_cents
1Heirloom1000
2Honeycrisp800
3Cherries1400
purchase
vendor_idstall_idshopper_idsold_onunitsrevenue
1112021-01-2122000
1222021-02-171800
2232021-06-021800
3342021-05-1322800

Expected output

Your answer should return 2 rows with the columns vendor_id.

Starter code (Pandas (Python))

import pandas as pd

def top_earning_vendor(stall: pd.DataFrame, purchase: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return purchase

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