Jul-08-2023, 12:00 PM
(This post was last modified: Jul-08-2023, 03:44 PM by deanhystad.)
i want to create in python a code to calculate the elo ratings of players, each row is a game, every game starts with 1500 points, and the game being calculated will have only the points added, on the next game, there is no draw, and the python code should use pandas and excel, the excel sheet.
.
.
import pandas as pd
from openpyxl import Workbook
def calculate_elo_rating(rating_a, rating_b, score_a, score_b, k_factor=32):
expected_score_a = 1 / (1 + 10 ** ((rating_b - rating_a) / 400))
expected_score_b = 1 / (1 + 10 ** ((rating_a - rating_b) / 400))
new_rating_a = rating_a + k_factor * (score_a - expected_score_a)
new_rating_b = rating_b + k_factor * (score_b - expected_score_b)
return new_rating_a, new_rating_b
# Read the data from Excel into a pandas DataFrame
df = pd.read_excel('games.xlsx')
# Initialize new columns for the updated ratings
df['New Rating A'] = df['Rating A']
df['New Rating B'] = df['Rating B']
# Iterate over the rows and calculate the Elo ratings
for index, row in df.iterrows():
rating_a = row['Rating A']
rating_b = row['Rating B']
score_a = row['Score A']
score_b = row['Score B']
new_rating_a, new_rating_b = calculate_elo_rating(rating_a, rating_b, score_a, score_b)
df.at[index, 'New Rating A'] = new_rating_a
df.at[index, 'New Rating B'] = new_rating_b
# Save the results to a new Excel file
df.to_excel('updated_ratings.xlsx', index=False)
deanhystad write Jul-08-2023, 03:44 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
