I have a pandas dataframe as follows:
Athlete ID | City | No. of Sport Fields |
---|---|---|
1231 | LA | 81 |
4231 | NYC | 80 |
2234 | NJ | 64 |
1223 | SF | 75 |
4531 | LA | 81 |
2345 | NYC. | 80 |
... |
I want to print the City and No. of Sport Fields columns and group by City and sort by No. of Sport Fields. groupby() won't work here because I am not calculating anything.
In your example, it seems that the No. of Sports Field remains the same for a given City.
You can therefore use first()
upon grouping by City, before sorting by No. of Sports Field :
df.groupby('City').first().sort_values(by='No. of Sport Fields')
This returns:
Athlete ID No. of Sport Fields
City
NJ 2234 64
SF 1223 75
NYC 4231 80
NYC. 2345 80
LA 1231 81