I have this Dataframe (sample):
| HorseId | FGrating | Date |
|---|---|---|
| 1736 | 110 | 03-09-2017 |
| 1736 | 124 | 16-09-2017 |
| 19373 | 144 | 01-10-2017 |
| 19373 | 137 | 22-10-2017 |
I need to extract the last FGrating of every horse. This means I need to extract the FGrating for the maximum date, grouped by HorseId.
I expect this result:
| HorseId | FGrating | Date |
|---|---|---|
| 1736 | 124 | 16-09-2017 |
| 19373 | 137 | 22-10-2017 |
After studying this link and this link, I came up with this code:
last_fg_rating = raw_data.groupby('HorseId')
last_fg_rating.apply(lambda x: x[x['Dato'] == x['Dato'].max()])
last_fg_rating = last_fg_rating[['Dato', 'HorseId', 'FGrating']]
But it doesn't give me the correct result.
Where am I mistaking? What should the correct code be?