Windowed Aggregation
Hard
pandas
Airbnb
DoorDash
Uber
Given a list of dictionaries, add a rank field to each row based on the value of a specified column within each group (partition). Rank 1 is the highest value. Ties receive the same rank.
Example
data = [
{"dept": "eng", "name": "Alice", "score": 90},
{"dept": "eng", "name": "Bob", "score": 80},
{"dept": "sales", "name": "Carol", "score": 95}
]
rank_within_group(data, "dept", "score")
# => [
# {"dept": "eng", "name": "Alice", "score": 90, "rank": 1},
# {"dept": "eng", "name": "Bob", "score": 80, "rank": 2},
# {"dept": "sales", "name": "Carol", "score": 95, "rank": 1}
# ]
Test Cases
Python Editor
Output
Click "Run" to see results...