0

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?

Bogdan Doicin
  • 2,252
  • 5
  • 26
  • 31
  • Similar to this: https://stackoverflow.com/questions/9850954/pandas-get-most-recent-value-of-a-particular-column-indexed-by-another-column/21007047#21007047 – Bogdan Doicin Nov 09 '21 at 12:19

0 Answers0