{ "cells": [ { "cell_type": "markdown", "id": "5c9991b1", "metadata": {}, "source": [ "Generate player votes database, containing votes for Serie A matchday and player.\n", "\n", "Votes data is manually downloaded from https://www.fantacalcio.it/voti-fantacalcio-serie-a\n", "\n", "Serie A calendar is loaded from another file, to add information not containing in votes files: home/away, team opponent." ] }, { "cell_type": "code", "execution_count": 1, "id": "b63beb69", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "a3c39376", "metadata": {}, "outputs": [], "source": [ "cal = np.array(pd.read_excel('fantacalcio/seriea_calendar.xlsx', header = None))\n", "\n", "cal_df = pd.DataFrame(columns = ['matchday', 'team1', 'team2'])\n", "\n", "matchday = 0\n", "\n", "for i in range(cal.shape[0]):\n", " if(cal[i, 0][0].isnumeric()):\n", " matchday = matchday + 1\n", " continue\n", " \n", " teams = cal[i, 0].split('-')\n", " \n", " frame = pd.DataFrame([[matchday, teams[0], teams[1]]], columns = cal_df.columns)\n", "\n", " cal_df = pd.concat([cal_df, frame], ignore_index = True)\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "92d3c6fb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
matchdayteam1team2
01FiorentinaCremonese
11VeronaNapoli
21JuventusSassuolo
31LazioBologna
41LecceInter
............
37538LecceBologna
37638SassuoloFiorentina
37738MilanVerona
37838TorinoInter
37938UdineseJuventus
\n", "

380 rows × 3 columns

\n", "
" ], "text/plain": [ " matchday team1 team2\n", "0 1 Fiorentina Cremonese\n", "1 1 Verona Napoli\n", "2 1 Juventus Sassuolo\n", "3 1 Lazio Bologna\n", "4 1 Lecce Inter\n", ".. ... ... ...\n", "375 38 Lecce Bologna\n", "376 38 Sassuolo Fiorentina\n", "377 38 Milan Verona\n", "378 38 Torino Inter\n", "379 38 Udinese Juventus\n", "\n", "[380 rows x 3 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal_df" ] }, { "cell_type": "code", "execution_count": 4, "id": "0d4a330d", "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(columns = ['matchday', 'player', 'team', 'oppteam', 'home', 'vote', 'goals', 'assists', 'cards_malus', 'fantavote'])\n", "\n", "LAST_MATCH = 19\n", "\n", "for matchday in range(1, LAST_MATCH + 1):\n", " \n", " votes_file = 'fantacalcio/voti/Voti_Fantacalcio_Stagione_2022_23_Giornata_' + str(matchday) + '.xlsx'\n", "\n", "\n", "\n", "\n", " rx = np.array(pd.read_excel(votes_file, header = None))\n", "\n", " read = 0\n", " for i in range(rx.shape[0]):\n", " if(rx[i, 0] == \"Cod.\"):\n", " read = 1\n", " team = rx[i-1, 0];\n", " continue\n", "\n", " if(read):\n", " if(isinstance(rx[i, 0], int)):\n", " if((isinstance(rx[i, 3], float) or isinstance(rx[i, 3], int)) and rx[i, 1] != \"ALL\") :\n", " player = rx[i, 2];\n", " vote = float(rx[i, 3])\n", " goals = rx[i, 4] + rx[i, 8] - rx[i, 5]\n", " assists = rx[i, 12]\n", " cards_malus = rx[i, 10] * 0.5 + rx[i, 11]\n", " \n", " oppteam = ''\n", " home = 0\n", " for j in range(cal_df.shape[0]):\n", " if(cal_df['matchday'][j] == matchday):\n", " if(cal_df['team1'][j] == team):\n", " oppteam = cal_df['team2'][j] \n", " home = 1\n", " elif(cal_df['team2'][j] == team):\n", " oppteam = cal_df['team1'][j] \n", " home = 0\n", "\n", " goals_gen = goals * 3;\n", " if(goals < 0):\n", " goals_gen = goals\n", " \n", " fantavote = vote + goals_gen + assists - cards_malus \n", " \n", " frame = pd.DataFrame([[matchday, player, team, oppteam, home, vote, goals, assists, cards_malus, fantavote]], columns = df.columns)\n", "\n", " df = pd.concat([df, frame], axis = 0, ignore_index = True)\n", " else:\n", " read = 0\n", " continue\n", "\n", " df" ] }, { "cell_type": "code", "execution_count": 5, "id": "6ade957d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
matchdayplayerteamoppteamhomevotegoalsassistscards_malusfantavote
01MussoAtalantaSampdoria06.0000.55.5
11ToloiAtalantaSampdoria07.0100.010.0
21DjimsitiAtalantaSampdoria06.0000.06.0
31HateboerAtalantaSampdoria06.0000.55.5
41OkoliAtalantaSampdoria05.5000.55.0
.................................
542319IlicVeronaLecce17.0010.08.0
542419Sulemana I.VeronaLecce16.0000.06.0
542519LasagnaVeronaLecce15.5000.05.5
542619KallonVeronaLecce16.0000.06.0
542719DjuricVeronaLecce16.0000.06.0
\n", "

5428 rows × 10 columns

\n", "
" ], "text/plain": [ " matchday player team oppteam home vote goals assists \\\n", "0 1 Musso Atalanta Sampdoria 0 6.0 0 0 \n", "1 1 Toloi Atalanta Sampdoria 0 7.0 1 0 \n", "2 1 Djimsiti Atalanta Sampdoria 0 6.0 0 0 \n", "3 1 Hateboer Atalanta Sampdoria 0 6.0 0 0 \n", "4 1 Okoli Atalanta Sampdoria 0 5.5 0 0 \n", "... ... ... ... ... ... ... ... ... \n", "5423 19 Ilic Verona Lecce 1 7.0 0 1 \n", "5424 19 Sulemana I. Verona Lecce 1 6.0 0 0 \n", "5425 19 Lasagna Verona Lecce 1 5.5 0 0 \n", "5426 19 Kallon Verona Lecce 1 6.0 0 0 \n", "5427 19 Djuric Verona Lecce 1 6.0 0 0 \n", "\n", " cards_malus fantavote \n", "0 0.5 5.5 \n", "1 0.0 10.0 \n", "2 0.0 6.0 \n", "3 0.5 5.5 \n", "4 0.5 5.0 \n", "... ... ... \n", "5423 0.0 8.0 \n", "5424 0.0 6.0 \n", "5425 0.0 5.5 \n", "5426 0.0 6.0 \n", "5427 0.0 6.0 \n", "\n", "[5428 rows x 10 columns]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 6, "id": "230213fe", "metadata": {}, "outputs": [], "source": [ "df.to_excel('mid_outputs/players_votes.xlsx')" ] }, { "cell_type": "markdown", "id": "ef91ece3", "metadata": {}, "source": [ "Elaborate data for past seasons.\n", "\n", "Do not repeat if data is already present." ] }, { "cell_type": "code", "execution_count": 7, "id": "da35bdf9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
matchdayteam1team2
01VeronaSassuolo
11InterGenoa
21EmpoliLazio
31TorinoAtalanta
41BolognaSalernitana
............
37538SpeziaNapoli
37638SassuoloMilan
37738InterSampdoria
37838SalernitanaUdinese
37938VeneziaCagliari
\n", "

380 rows × 3 columns

\n", "
" ], "text/plain": [ " matchday team1 team2\n", "0 1 Verona Sassuolo\n", "1 1 Inter Genoa\n", "2 1 Empoli Lazio\n", "3 1 Torino Atalanta\n", "4 1 Bologna Salernitana\n", ".. ... ... ...\n", "375 38 Spezia Napoli\n", "376 38 Sassuolo Milan\n", "377 38 Inter Sampdoria\n", "378 38 Salernitana Udinese\n", "379 38 Venezia Cagliari\n", "\n", "[380 rows x 3 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal_df = pd.read_excel('fantacalcio/season2122/seriea_calendar.xlsx')\n", "\n", "to_drop = list()\n", "\n", "for i in range(cal_df.shape[0]):\n", " if(not isinstance(cal_df['matchday'][i], int)):\n", " to_drop.append(i)\n", " \n", "cal_df = cal_df.drop(to_drop)\n", "\n", "cal_df = cal_df.reset_index(drop = True)\n", "\n", "cal_df" ] }, { "cell_type": "code", "execution_count": 8, "id": "0313a0f4", "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(columns = ['matchday', 'player', 'team', 'oppteam', 'home', 'vote', 'goals', 'assists', 'cards_malus', 'fantavote'])\n", "\n", "LAST_MATCH = 38\n", "\n", "for matchday in range(1, LAST_MATCH + 1):\n", " \n", " votes_file = 'fantacalcio/season2122/voti/Voti_Fantacalcio_Stagione_2021_22_Giornata_' + str(matchday) + '.xlsx'\n", "\n", "\n", "\n", "\n", " rx = np.array(pd.read_excel(votes_file, header = None))\n", "\n", " read = 0\n", " for i in range(rx.shape[0]):\n", " if(rx[i, 0] == \"Cod.\"):\n", " read = 1\n", " team = rx[i-1, 0];\n", " continue\n", "\n", " if(read):\n", " if(isinstance(rx[i, 0], int)):\n", " if((isinstance(rx[i, 3], float) or isinstance(rx[i, 3], int)) and rx[i, 1] != \"ALL\") :\n", " player = rx[i, 2];\n", " vote = float(rx[i, 3])\n", " goals = rx[i, 4] + rx[i, 8] - rx[i, 5]\n", " assists = rx[i, 12]\n", " cards_malus = rx[i, 10] * 0.5 + rx[i, 11]\n", " \n", " oppteam = ''\n", " home = 0\n", " for j in range(cal_df.shape[0]):\n", " if(cal_df['matchday'][j] == matchday):\n", " if(cal_df['team1'][j] == team):\n", " oppteam = cal_df['team2'][j] \n", " home = 1\n", " elif(cal_df['team2'][j] == team):\n", " oppteam = cal_df['team1'][j] \n", " home = 0\n", "\n", " goals_gen = goals * 3;\n", " if(goals < 0):\n", " goals_gen = goals\n", " \n", " fantavote = vote + goals_gen + assists - cards_malus \n", " \n", " frame = pd.DataFrame([[matchday, player, team, oppteam, home, vote, goals, assists, cards_malus, fantavote]], columns = df.columns)\n", "\n", " df = pd.concat([df, frame], axis = 0, ignore_index = True)\n", " else:\n", " read = 0\n", " continue\n", "\n", " df" ] }, { "cell_type": "code", "execution_count": 9, "id": "82b1cfc8", "metadata": {}, "outputs": [], "source": [ "df.to_excel('mid_outputs/season2122/players_votes.xlsx')" ] }, { "cell_type": "code", "execution_count": 10, "id": "d1fca3ba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
matchdayteam1team2
01FiorentinaTorino
11VeronaRoma
21ParmaNapoli
31GenoaCrotone
41SassuoloCagliari
............
37538SassuoloLazio
37638AtalantaMilan
37738TorinoBenevento
37838BolognaJuventus
37938NapoliVerona
\n", "

380 rows × 3 columns

\n", "
" ], "text/plain": [ " matchday team1 team2\n", "0 1 Fiorentina Torino\n", "1 1 Verona Roma\n", "2 1 Parma Napoli\n", "3 1 Genoa Crotone\n", "4 1 Sassuolo Cagliari\n", ".. ... ... ...\n", "375 38 Sassuolo Lazio\n", "376 38 Atalanta Milan\n", "377 38 Torino Benevento\n", "378 38 Bologna Juventus\n", "379 38 Napoli Verona\n", "\n", "[380 rows x 3 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal_df = pd.read_excel('fantacalcio/season2021/seriea_calendar.xlsx')\n", "\n", "to_drop = list()\n", "\n", "for i in range(cal_df.shape[0]):\n", " if(not isinstance(cal_df['matchday'][i], int)):\n", " to_drop.append(i)\n", " \n", "cal_df = cal_df.drop(to_drop)\n", "\n", "cal_df = cal_df.reset_index(drop = True)\n", "\n", "cal_df" ] }, { "cell_type": "code", "execution_count": 11, "id": "6d5d4101", "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(columns = ['matchday', 'player', 'team', 'oppteam', 'home', 'vote', 'goals', 'assists', 'cards_malus', 'fantavote'])\n", "\n", "LAST_MATCH = 38\n", "\n", "for matchday in range(1, LAST_MATCH + 1):\n", " \n", " votes_file = 'fantacalcio/season2021/voti/Voti_Fantacalcio_Stagione_2020_21_Giornata_' + str(matchday) + '.xlsx'\n", "\n", "\n", "\n", "\n", " rx = np.array(pd.read_excel(votes_file, header = None))\n", "\n", " read = 0\n", " for i in range(rx.shape[0]):\n", " if(rx[i, 0] == \"Cod.\"):\n", " read = 1\n", " team = rx[i-1, 0];\n", " continue\n", "\n", " if(read):\n", " if(isinstance(rx[i, 0], int)):\n", " if((isinstance(rx[i, 3], float) or isinstance(rx[i, 3], int)) and rx[i, 1] != \"ALL\") :\n", " player = rx[i, 2];\n", " vote = float(rx[i, 3])\n", " goals = rx[i, 4] + rx[i, 8] - rx[i, 5]\n", " assists = rx[i, 12]\n", " cards_malus = rx[i, 10] * 0.5 + rx[i, 11]\n", " \n", " oppteam = ''\n", " home = 0\n", " for j in range(cal_df.shape[0]):\n", " if(cal_df['matchday'][j] == matchday):\n", " if(cal_df['team1'][j] == team):\n", " oppteam = cal_df['team2'][j] \n", " home = 1\n", " elif(cal_df['team2'][j] == team):\n", " oppteam = cal_df['team1'][j] \n", " home = 0\n", "\n", " goals_gen = goals * 3;\n", " if(goals < 0):\n", " goals_gen = goals\n", " \n", " fantavote = vote + goals_gen + assists - cards_malus \n", " \n", " frame = pd.DataFrame([[matchday, player, team, oppteam, home, vote, goals, assists, cards_malus, fantavote]], columns = df.columns)\n", "\n", " df = pd.concat([df, frame], axis = 0, ignore_index = True)\n", " else:\n", " read = 0\n", " continue\n", "\n", " df" ] }, { "cell_type": "code", "execution_count": 12, "id": "0a4bbffd", "metadata": {}, "outputs": [], "source": [ "df.to_excel('mid_outputs/season2021/players_votes.xlsx')" ] }, { "cell_type": "code", "execution_count": null, "id": "48a341c2", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }