I have an expression in Pandas where I sort the top three values by country:
Country | Value
---------------------|------
Germany | 102.1
Germany | 90.3
Germany | 44.6
Switzerland | 59.9
Switzerland | 35.3
Switzerland | 21.6
...and so on
which I obtained using df.groupby("Country").head(3)[["Country", "Value"]]. Now, I'd like to append a third column that associates the rank within the country to the value:
Country | Value | Rank
---------------------|--------|------
Germany | 102.1 | 1
Germany | 90.3 | 2
Germany | 44.6 | 3
Switzerland | 59.9 | 1
Switzerland | 35.3 | 2
Switzerland | 21.6 | 3
...and so on
How would I best go about doing this?