I have a CSV dataset of tennis match results in winner, loser format that has similar structure to this one: https://www.kaggle.com/jordangoblet/atp-tour-20002016
I want to create another table that will show number of wins, losses, days since last match, average serve percentage etc. per player.
A friend suggested trying something like this:
games = [
["Elliot", 7, "Maya", 10],
["Elliot", 10, "Brendan", 8],
["Jamie", 7, "Omid", 10],
["Elliot", 6, "Jamie", 10],
["Brendan", 7, "Jamie", 10],
]
players = {}
K = 32.0
game_number = 1
for game in games:
p1name, p1score, p2name, p2score = game
if p1name not in players:
players[p1name] = {
'wins': 0,
'losses': 0,
'score': 1000.0,
}
if p2name not in players:
players[p2name] = {
'wins': 0,
'losses': 0,
'score': 1000.0,
}
more code...
...
print('Rank Player Elo Wins Losses')
rank = 1
for player, p in sorted(players.items(), key=lambda item: (item[1]['score'], item[0]), reverse=True):
print('%4s %20s %7.1f %5s %7s' % (rank, player, p['score'], p['wins'], p['losses']))
rank += 1
I tried to tweak this code to my needs by making my df a list of lists:
lol = df.values.tolist()
players = {}
for game in lol:
game_id, tournament_name, tournament_id, tournament_date, round_match, winner, winner_id, loser, loser_id, winner_elo,
loser_elo, winner_delta, loser_delta, winner_set_1, loser_set_1, winner_set_2, loser_set_2, winner_set_3, loser_set_3,
winner_set_4, loser_set_4, winner_set_5, loser_set_5, winner_sets, loser_sets, winner_serve_points_won,
loser_serve_points_won, winner_serve_hold, loser_serve_hold, winner_points_won, loser_points_won, winner_true_serve_pct,
loser_true_serve_pct = game
where all the above are the column names in my original df
but I get this error:
NameError: name "game_id" is not defined
The original code works fine but I don't understand why the =game doesn't work on my case.
Am I missing something? Is there a better approach to this problem? My dataset has around 60k instances and 33 columns
Edit: Got the solution, the variables = game should be just in one line. But you're welcome to comment if there is a better solution to achieve what I described in the comments
Edit: columns info
game/tournament/winner/loser_id = unique id's
tournament_name = matches are played in daily tournaments
tournament_date = date of tournament/match
round_match = round of tournament the match takes place
winner/loser= winner/loser name
winner/loser_elo = current elo for the day
winner/loser_delta = delta change after match
winner/loser_set_1,2,3,4,5 = score for winner/loser per game, match ends when a player reaches 3 games, 11 points needed to win a game or 2 point differential (extra points if game is tied at 10-10)
winner/loser_sets= number of sets
winner/loser_points_won = total points per player in the match
winner/loser_true_serve_pct = % of points player wins when he is serving
winner_serve_points_won/loser_serve_points_won is obsolete
winner_serve_hold/loser_serve_hold will be replaced by winner/loser_true_serve_pct