import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
data=pd.read_csv('wingbacks_topteams.csv')
# rename 2 unnamed columns
data.rename(columns={'Unnamed: 0':'league','Unnamed: 1': 'season'},inplace= True)
data.drop('Unnamed: 2',1,inplace=True)
data.head(10)
leagues=['EPL','La_liga','Bundesliga','Serie_A','Ligue_1']
seasons=[2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021]
# Data is grouped by `league` and `season`, then taking the sum of stats
sum_df = data.groupby(['league','season']).sum().reset_index()
sum_df.drop(['player_id','team_id'],axis = 1, inplace = True)
# Round the decimal to 2 digits
sum_df.round(2)
funct = lambda row: row.xGChain*90/row.time # Define a function for the new column
col = sum_df.apply(funct, axis=1) # Get column data with an index
df = sum_df.assign(xGChain90=col.values) # Assign values to column 'xGChain90'
# Format the float numbers for better reading
df.round(2)
fig = px.line(df, x="season", y="xGChain90",color="league", render_mode="svg")
fig.show()
df.round(2)
# Divide seasons into groups
period1=[2014, 2015, 2016, 2017]
period2=[2018, 2019, 2020, 2021]
# Filter out info in the first period then take the mean of `xGChain90`
df1 = df.loc[df['season'].isin(period1)]
a = df1.groupby('league').mean().reset_index()
# We only need 2 columns `league` and `xGChain90`
b = a.loc[:,['league', 'xGChain90']]
# Add a column called "period" for better view
b["period"] = "2014 - 2017"
# Same actions for the second period
df2 = df.loc[df['season'].isin(period2)]
c = df2.groupby('league').mean().reset_index()
d = c.loc[:,['league', 'xGChain90']]
d["period"] = "2018 - 2021"
# Concatenate 2 dataframes into one and call it df_mean
df_mean =pd.concat([b,d])
df_mean.round(2)
fig_4years=px.histogram(df_mean,x='league',y='xGChain90', color='period',barmode='group',title='Wingbacks',
color_discrete_map={#"2014-2017":"blue",
#"2018-2021":"yellow"
}
)
fig_4years.update_layout(yaxis_title="xGChain90")
fig_4years.update_xaxes(type='category')
fig_4years.show()
# take mean value of all stats
mean_each_season_allstats = df.groupby(['season']).mean().reset_index()
# filter out only two columns "season" and "xGChain90"
xGChain90_mean = mean_each_season_allstats.loc[:,['season', 'xGChain90']]
xGChain90_mean.round(2)
xGChain90_mean.drop([7],inplace = True)
xGChain90_mean.round(2)
fig = px.line(xGChain90_mean, x="season", y="xGChain90", render_mode="svg")
fig.show()
data.head(10)
sum_data = data.groupby(['player']).sum().reset_index()
# delete unnecessary columns
sum_data.drop(['season','team_id','player_id'],axis = 1, inplace = True)
# check
sum_data.head(10)
# divide columns by `time`
sum_data90 = sum_data[['goals', 'assists', 'key_passes', 'xG', 'xA', 'shots',
'xGChain', 'xGBuildup']].div(sum_data.time, axis=0)
# then multiply with 90
sum_data90[['goals', 'assists', 'key_passes', 'xG', 'xA', 'shots',
'xGChain', 'xGBuildup']] = sum_data90[['goals', 'assists', 'key_passes', 'xG', 'xA', 'shots',
'xGChain', 'xGBuildup']].apply(lambda x: x*90)
# rename the columns
sum_data90.columns.values[[0, 1, 2, 3, 4, 5, 6, 7]] = ['goals90', 'assists90', 'key_passes90', 'xG90', 'xA90', 'shots90',
'xGChain90', 'xGBuildup90']
sum_data90.head(10)
best_wingbacks = pd.concat([sum_data, sum_data90], axis = 1)
best_wingbacks.drop(['goals', 'assists', 'key_passes', 'xG', 'xA', 'shots',
'xGChain', 'xGBuildup'], axis = 1, inplace = True)
best_wingbacks.head()
# remove all `player` with `time` less than 5000 minutes and sort the rest
rank_wingbacks = best_wingbacks.drop(best_wingbacks[best_wingbacks['time']<5000].index)
rank_wingbacks.sort_values('xGChain90',ascending=False,inplace=True)
rank_wingbacks.reset_index(inplace=True, drop=True)
# add a column called `rank`
rank_wingbacks['rank'] = range(1,len(rank_wingbacks)+1)
# rearrange the columns
rank_wingbacks = rank_wingbacks[['rank', 'player', 'time', 'xGChain90', 'xGBuildup90', 'goals90', 'assists90', 'key_passes90', 'xG90', 'xA90', 'shots90']]
# round to 3 decimal digits
rank_wingbacks = rank_wingbacks.round(3)
rank_wingbacks.head(20)
from mplsoccer import Radar, FontManager
Davies_df = rank_wingbacks.loc[rank_wingbacks['player']=='Alphonso Davies'][['xGChain90', 'xGBuildup90', 'goals90', 'assists90', 'key_passes90', 'xG90', 'xA90', 'shots90']]
Davies = Davies_df.values.tolist()[0]
Davies
# Names of the stats
stats = ['xGChain90', 'xGBuildup90', 'goals90', 'assists90', 'key_passes90', 'xG90', 'xA90', 'shots90']
# The lower and upper bounds for the stats
low = [0.269, 0.213, 0.0, 0.053, 0.583, 0.009, 0.056, 0.12]
high = [0.839, 0.672, 0.275, 0.343, 2.306, 0.173, 0.309, 1.832]
# Make a radar
radar = Radar(stats, low, high,
round_int=[False]*8,
num_rings=4,
ring_width=4, center_circle_radius=1)
URL1 = ('https://github.com/googlefonts/SourceSerifProGFVersion/blob/main/'
'fonts/SourceSerifPro-Regular.ttf?raw=true')
serif_regular = FontManager(URL1)
URL2 = ('https://github.com/googlefonts/SourceSerifProGFVersion/blob/main/'
'fonts/SourceSerifPro-ExtraLight.ttf?raw=true')
serif_extra_light = FontManager(URL2)
URL3 = ('https://github.com/google/fonts/blob/main/ofl/rubikmonoone/'
'RubikMonoOne-Regular.ttf?raw=true')
rubik_regular = FontManager(URL3)
URL4 = 'https://github.com/googlefonts/roboto/blob/main/src/hinted/Roboto-Thin.ttf?raw=true'
robotto_thin = FontManager(URL4)
URL5 = 'https://github.com/googlefonts/roboto/blob/main/src/hinted/Roboto-Regular.ttf?raw=true'
robotto_regular = FontManager(URL5)
URL6 = 'https://github.com/googlefonts/roboto/blob/main/src/hinted/Roboto-Bold.ttf?raw=true'
robotto_bold = FontManager(URL6)
fig, ax = radar.setup_axis() # format axis as a radar
rings_inner = radar.draw_circles(ax=ax, facecolor='#ffb2b2', edgecolor='#fc5f5f') # draw circles
radar_output = radar.draw_radar(Davies, ax=ax,
kwargs_radar={'facecolor': '#aa65b2'},
kwargs_rings={'facecolor': '#66d8ba'}) # draw the radar
radar_poly, rings_outer, vertices = radar_output
range_labels = radar.draw_range_labels(ax=ax, fontsize=15,
fontproperties=robotto_thin.prop) # draw the range labels
param_labels = radar.draw_param_labels(ax=ax, fontsize=15,
fontproperties=robotto_regular.prop) # draw the param labels
Arnold_df = rank_wingbacks.loc[rank_wingbacks['player']=='Trent Alexander-Arnold'][['xGChain90', 'xGBuildup90', 'goals90', 'assists90', 'key_passes90', 'xG90', 'xA90', 'shots90']]
Arnold = Arnold_df.values.tolist()[0]
Arnold
fig, ax = radar.setup_axis()
rings_inner = radar.draw_circles(ax=ax, facecolor='#ffb2b2', edgecolor='#fc5f5f')
radar_output = radar.draw_radar(Arnold, ax=ax,
kwargs_radar={'facecolor': '#aa65b2'},
kwargs_rings={'facecolor': '#66d8ba'})
radar_poly, rings_outer, vertices = radar_output
range_labels = radar.draw_range_labels(ax=ax, fontsize=15,
fontproperties=robotto_thin.prop)
param_labels = radar.draw_param_labels(ax=ax, fontsize=15,
fontproperties=robotto_regular.prop)
Rafael_df = rank_wingbacks.loc[rank_wingbacks['player']=='Raphael Guerreiro'][['xGChain90', 'xGBuildup90', 'goals90', 'assists90', 'key_passes90', 'xG90', 'xA90', 'shots90']]
Rafael = Rafael_df.values.tolist()[0]
Rafael
fig, ax = radar.setup_axis()
rings_inner = radar.draw_circles(ax=ax, facecolor='#ffb2b2', edgecolor='#fc5f5f')
radar_output = radar.draw_radar(Rafael, ax=ax,
kwargs_radar={'facecolor': '#aa65b2'},
kwargs_rings={'facecolor': '#66d8ba'})
radar_poly, rings_outer, vertices = radar_output
range_labels = radar.draw_range_labels(ax=ax, fontsize=15,
fontproperties=robotto_thin.prop)
param_labels = radar.draw_param_labels(ax=ax, fontsize=15,
fontproperties=robotto_regular.prop)