Median-Priced Listing in Each Neighborhood
Problem
You are given a DataFrame `listing` describing real-estate listings.
```text
+--------------+---------+
| Column | Type |
+--------------+---------+
| listing_ref | int |
| neighborhood | object |
| price | int |
+--------------+---------+
listing_ref is the unique key of this DataFrame.
```
Within each `neighborhood`, find the listing(s) at the **median** price. When a neighborhood holds an odd number of listings the median is the single middle one; when it holds an even number, both middle listings are returned. Order listings inside a neighborhood by `price` ascending, breaking ties by `listing_ref` ascending, and use that order to locate the middle position(s).
Return a DataFrame with columns `listing_ref`, `neighborhood`, `price`. Result order does not matter.
Input data
Example rows — the live problem includes the full dataset.
| listing_ref | neighborhood | price |
|---|---|---|
| 1 | Harborside | 300 |
| 2 | Harborside | 500 |
| 3 | Harborside | 700 |
| 4 | Maplewood | 420 |
| 5 | Maplewood | 460 |
Expected output
Your answer should return 3 rows with the columns listing_ref, neighborhood, price.
Starter code (Pandas (Python))
import pandas as pd
def median_listing(listing: pd.DataFrame) -> pd.DataFrame:
# Your code here
return listingSolve 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