Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Elo rating
#1
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.
Reply
#2
Do you have a question?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to scrape and count star rating using Selenium and Python? celinafregoso99 1 3,346 Feb-15-2021, 02:45 PM
Last Post: kashcode

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020