Python
#40

Select Top N Records Per Group with nlargest() in Pandas

MediumAmazon
Interview Question #40
Asked at Amazon

Given an e-commerce catalog, select the top 2 highest-priced products for each `department`.

Input Table: products
4 rows preview
product_iddepartmentprice
1Tech1200
2Tech800
3Tech400
4Books35
Expected Output Structure3 rows
departmentproduct_idprice
Tech11200
Tech2800
Books435
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

Python 3.10 (Pandas)
Environment Ready

Click "Run & Test" to execute your Pandas code against the test assertions.

Chat with us