Sure, if you have a DataFrame that contains player ratings and match outcomes, you can apply the compute_elo function to it. Here's an example:
Let's assume you have a DataFrame df with columns 'player_rating', 'opponent_rating', and 'outcome'.
import pandas as pd
# Suppose this is your DataFrame
df = pd.DataFrame({
'player_rating': [2000, 2100, 2200],
'opponent_rating': [1900, 2050, 2300],
'outcome': [1, 0.5, 0]
})
# Define the compute_elo function
def compute_elo(player_rating, opponent_rating, outcome, k=32):
player_expected_outcome = 1 / (1 + 10 ** ((opponent_rating - player_rating) / 400))
player_new_rating = player_rating + k * (outcome - player_expected_outcome)
return player_new_rating
# Apply the function to each row in the DataFrame
df['new_player_rating'] = df.apply(lambda row: compute_elo(row['player_rating'], row['opponent_rating'], row['outcome']), axis=1)
In this code, we use the apply function to apply the compute_elo function to each row of the DataFrame. The lambda function takes a row of the DataFrame as input and passes the player rating, opponent rating, and outcome to the compute_elo function.
The result is a new column 'new_player_rating' in the DataFrame containing the updated player ratings after each match.