Practice / window functions

Top Two, Ties Included

hard

A sales director wants to reward the reps ranked 1st or 2nd by deal count in each region, using competition ranking: reps with equal deals share a rank, and the next rank skips ahead (after two reps tie for 1st, the following rep is 3rd, not 2nd).

sales

Your task: return region, rep, and deals for every rep whose competition rank within their region is 1 or 2. Reps tied inside the top two all qualify; a rep pushed down to 3rd by a tie above them does not.

Assign your answer DataFrame to result. Row order doesn't matter.

salesinput DataFrame

Schema
columntype
regionstring
repstring
dealslong
Sample rows
regionrepdeals
EastAna10
EastBo8
EastCy8
WestDi7
WestEd7
WestFi5
Expected output shape
regionrepdeals· 5 rows
Hint

Use F.rank() (competition ranking), partitioned by region, ordered by deals descending. Keep rk <= 2. Ties share a rank, and the rank after a tie skips ahead — that is exactly what tells rank apart from dense_rank.

Lesson refresher

This problem builds on Window Functions (~9 min). Pop it open in a new tab if you want a quick recap.

Loading editor…
Hit Run to execute your code and see the output here.