AnalystPath

Second-Priciest Listing per Neighborhood

PandasMediumMid level~10 min

Problem

A property portal gives you a DataFrame `listings` with an asking_price and the neighborhood each listing sits in. For each neighborhood, find the listings whose asking_price is the second-highest distinct price in that neighborhood. If several listings share that second-highest price, return all of them. Skip any neighborhood that has fewer than two distinct price levels. Return a DataFrame with columns listing_id and neighborhood, ordered by listing_id.

Input data

Example rows — the live problem includes the full dataset.

listings
listing_idasking_priceneighborhood
170000Harborview
280000Harborview
380000Harborview
490000Harborview
555000Old Mill

Expected output

Your answer should return 4 rows with the columns listing_id, neighborhood.

Starter code (Pandas (Python))

import pandas as pd

def second_priciest_listing_per_neighborhood(listings) -> pd.DataFrame:
    # Your code here
    return listings

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