I had a distance column in my dataset in which the valuses where appearing as 400m or…
- User
I had a distance column in my dataset in which the valuses where appearing as 400m or 575m, I wanted to remove the m and jsut keep the value of the distance so just 400 and 575 in a new column, I used the below code which worked:
Extract distance in meters from 'Dis' column
df['Distance'] = df['Dis'].str.extract('(\d+)').astype(int)
Now, I want to do something similar for my Fin column which Is my race finished column in which the current values are listed as 1st, 2nd, 3rd, 4th, 5th and 6th. I just want to extreact the finish number so 1, 2, 3, 4, 5, 6
- Assistant
.
Extract finish number from 'Fin' column
df['Finish'] = df['Fin'].str.extract('(\d+)').astype(int)
- User
So, in my dataset, I have the below columns for a dog which is DogName called DogA, DogA, has 10 previous dog races and I have DogA's past form, I want to try understand a few things, for example below all is my columns: Date datetime64[ns] Track object Dis object Trp int64 Split float64 Bends int64 Fin object By object Win/Sec object Remarks object WnTm float64 Gng object Wght float64 SP float64 BSP float64 Grade object CalTm float64 DogName object GradeNumeric float64 Category object GradeNumber int32 Distance int32 Finish int32
And, lets say I want to try see what the recent form of DogA is, when I use the calculation below, it gives me Nans, so I am trying to udnerstand what would be the best approach or how would i use the values from the below code or interpret them in a model for example, or what is the best way to utltise the values from these, as somtimes for example a dog could have just 4 races or even 3 but we still want to know how its recent form is looking:
- Assistant
The reason you are getting NaNs is because the rolling function is taking the average of the last 5 values of the 'Finish' column, which may not exist for the first few rows.
To calculate the recent form of DogA, you could add a filter condition to your rolling function to calculate the average finish of DogA's last 5 races. Something like:
pythonrecent_form = df[df['DogName'] == 'DogA']['Finish'].rolling(window=5).mean()This will calculate the rolling average of the 'Finish' column only for rows where 'DogName' is 'DogA'.
Additionally, you could add a condition to only consider races from a certain time period, such as the last month or the last 10 races.
Once you have the recent form of DogA, you could use it as a feature in a model to predict the outcome of future races. You could also calculate the average recent form for all dogs and use this as a feature to identify which dogs have been performing well recently.
- User
can you explain how i can caclulate the below then, also assuming I have multiple different dogs in the DogName column:
This will calculate the rolling average of the ‘Finish’ column only for rows where ‘DogName’ is ‘DogA’.
Additionally, you could add a condition to only consider races from a certain time period, such as the last month or the last 10 races.
Once you have the recent form of DogA, you could use it as a feature in a model to predict the outcome of future races. You could also calculate the average recent form for all dogs and use this as a feature to identify which dogs have been performing well recently.
- Assistant
To calculate the rolling average of the 'Finish' column for each dog separately and for a specific time period, you can do the following:
python - User
Using my columns, how could I create a feature exactly like the below:
'Distance_Places_All'---The average distance of races were the greyhound finished 1st or 2nd. This statistic should indicate a distance preference for each greyhound. For example, if a greyhound has a statistic of 474.0 metres, he appears to perform well at longer distances than say 400 if the race we wanted to test was say a 400 race.
I have a column which is called Distance Distance and values are listed like 400, 575 etc and I have another column which is called Finish which tells the place the dog finished in that race it will be a value like 1 , 2, 3, 4, 5 or 6 depending on where the dog finished. Remember DogName would include the dog and the Date is the date of the race for each dog, the data set contains multiple different dogs and all their historic or previous races
- Assistant
To create the 'Distance_Places_All' feature, you can do the following:
python