import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from footyscripts import footyviz
#plotting settings
%matplotlib inline
matches = ['2013-08-3266-Barcelona_VS_Levante',
'2013-08-3278-Málaga_VS_Barcelona',
'2014-08-8360-Burnley_VS_Chelsea',
'2014-08-8370-Chelsea_VS_Leicester']
df = pd.read_csv("../datasets/_opta/_opta_data_.csv", encoding='utf-8', index_col=0)
df = df[df['match'].isin(matches)].sort(['match', '@minsec'])
df = df[df['@player_id']==306]
df.fillna(False, inplace=True)
#add to footyviz !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
x_size = 105.0
y_size = 68.0
df['x']=df['x']/100*x_size
df['y']=df['y']/100*y_size
df['to_x']=df['to_x']/100*x_size
df['to_y']=df['to_y']/100*y_size
-c:4: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead -c:5: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead -c:6: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead -c:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead
fig, ax = footyviz.draw_pitch()
footyviz.draw_events(df[(df.type=='all_passes') & (df.team_name=='Barcelona')], base_color='red', alpha=0.8)
footyviz.draw_events(df[(df.type=='all_passes') & (df.team_name=='Chelsea')], base_color='#235c96', alpha=0.8)
fig.savefig('fabregas.jpg')
df['success_pct'] = df['@type']=='completed'
df['count'] = 1
df[df.type=='all_passes'].groupby('team_name').agg({'x': [np.mean, np.std],
'assists': np.sum,
'long_ball': np.sum,
'count': np.sum,
'success_pct': np.mean})
count | x | success_pct | assists | long_ball | ||
---|---|---|---|---|---|---|
sum | mean | std | mean | sum | sum | |
team_name | ||||||
Barcelona | 86 | 67.527209 | 16.035325 | 0.825581 | 6 | 6 |
Chelsea | 166 | 55.232530 | 16.986273 | 0.903614 | 5 | 22 |
2 rows × 6 columns
df['fifth'] = np.digitize(df['x'], np.arange(0, 105, 105.0/5))
df[df.type=='all_passes'].groupby(['team_name', 'fifth']).agg({'assists': np.sum,
'long_ball': np.sum,
'count': np.sum})
count | assists | long_ball | ||
---|---|---|---|---|
team_name | fifth | |||
Barcelona | 1 | 1 | 0 | 0 |
2 | 3 | 1 | 1 | |
3 | 30 | 0 | 3 | |
4 | 40 | 1 | 2 | |
5 | 12 | 4 | 0 | |
Chelsea | 1 | 4 | 0 | 1 |
2 | 37 | 0 | 5 | |
3 | 69 | 1 | 15 | |
4 | 49 | 2 | 1 | |
5 | 7 | 2 | 0 |
10 rows × 3 columns