I'm assigning an incremental rank based on a value, but need to assign the same rank to duplicate values and maintain the overall sequence.
Instead of this:
Value | Rank |
---|---|
400 | 1 |
500 | 2 |
175 | 3 |
250 | 4 |
120 | 5 |
250 | 6 |
I need this:
Value | Rank |
---|---|
500 | 1 |
400 | 2 |
250 | 3 |
250 | 3 |
175 | 5 |
120 | 6 |
I'm actually doing this in an ArcGIS Pro toolbox script.
So far I've only got this far, which doesn't address the duplicates:
d = {'value': [500,120,175,400,250,250], 'rank': [0,0,0,0,0,0]
df = pd.DataFrame(data=d)
rank1 = df['value']
rank1 = rank1.sort_values(ascending=False)
rank1 = rank1.to_numpy()
rank2 = np.arange(1,len(rank1)+1,1)
rank1: [500 400 250 250 175 120] rank2: [1 2 3 4 5 6]
Desired output is: rank2: [1 2 3 3 5 6]
You can use rank
:
df["rank"] = df["value"].rank(method="min", ascending=False).astype(int)
value rank
0 500 1
1 120 6
2 175 5
3 400 2
4 250 3
5 250 3