I‘m working with some data and here I want to get the ranks (finishing position) for each horse in its recent runs (up to 6 runs before the run). The date of the run is defined as 'race_id'.
Is there a way to use groupby and agg but only aggregates the previous 6 values?
The data frame is as follows:
finishing_position horse_id race_id
1 K01 2014011
2 K02 2014011
3 M01 2014011
4 K01 2014012
2 K01 2014021
3 K01 2014031
1 M01 2015011
2 K01 2016012
1 K02 2016012
3 M01 2016012
4 J01 2016012
I want the result to be
finishing_position horse_id race_id recent
1 K01 2014011
2 K02 2014011
3 M01 2014011
4 K01 2014012 1
2 K01 2014021 1/4
3 K01 2014031 1/4/2
1 M01 2015011 3
2 K01 2016012 1/4/2/3
1 K02 2016012 2
3 M01 2016012 3/1
4 J01 2016012