AnalystPath

Median-Priced Listing in Each Neighborhood

PandasHardSenior level~10 min

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
listing_refneighborhoodprice
1Harborside300
2Harborside500
3Harborside700
4Maplewood420
5Maplewood460

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 listing

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