Package: xgclient
Python xG ClientPython client forfootball (soccer) expected goals (xG) statistics API.
It provides a list of events with xG metric for every game of more than 80 leagues.UsageTo install the latest version ofxgclientusepip.pipinstallxgclientExample usageBasic usagefromxgclient.clientimportExpectedGoalsClientclient=ExpectedGoalsClient('Your API Key')countries=client.countries()# list of countriestournaments=client.tournaments(country_id)# list of leagues for specified countryseasons=client.seasons(league_id)# list of seasons for specified leaguefixtures=client.fixtures(season_id)# list of fixtures for specified seasonfixture=client.fixture(fixture_id)# get one fixtureCalculating xg90 (expected goals for 90 minutes) metric for every team of available seasonsimportoperatorfromxgclient.clientimportExpectedGoalsClientclient=ExpectedGoalsClient('Your API key')forcountryinclient.countries():fortournamentinclient.tournaments(country['id']):forseasoninclient.seasons(tournament['id']):print(country['name'],tournament['name'],season['name'])print('=====')season_fixtures=client.fixtures(season['id'])expected_goals={}minutes={}team_names={}forfixtureinseason_fixtures:ifnotteam_names.get(fixture['homeTeam']['id']):team_names[fixture['homeTeam']['id']]=fixture['homeTeam']['name']minutes[fixture['homeTeam']['id']]=0ifnotteam_names.get(fixture['awayTeam']['id']):team_names[fixture['awayTeam']['id']]=fixture['awayTeam']['name']minutes[fixture['awayTeam']['id']]=0fixture_duration=fixture['duration']['firstHalf']+fixture['duration']['secondHalf']minutes[fixture['homeTeam']['id']]+=fixture_durationminutes[fixture['awayTeam']['id']]+=fixture_durationforeventinfixture['events']:ifnotevent['xg']:continueifnotexpected_goals.get(event['teamId']):expected_goals[event['teamId']]=0expected_goals[event['teamId']]+=event['xg']result={}forteam_idinexpected_goals:result[team_id]=(expected_goals[team_id]/minutes[team_id])*90result=sorted(result.items(),key=operator.itemgetter(1),reverse=True)forteam_id,valueinresult:print(team_names[team_id],value)print('')Example Output:England Premier League 2016/2017
=====
Manchester City 2.2112692731277535
Tottenham 2.052839403973515
Chelsea 1.826269731376351
Arsenal 1.799702725020647
Liverpool 1.69972352778546
Manchester Utd 1.6932413793103451
Southampton 1.439378453038676
Everton 1.3932328539823016
Bournemouth 1.2910729023383791
Stoke 1.2596034150371813
Leicester 1.212548156301597
West Ham 1.2049150684931513
Crystal Palace 1.1981870860927168
Swansea 1.0498671831765367
Burnley 0.9535088202866603
Watford 0.9309592061742009
West Brom 0.9158252695604089
Sunderland 0.9000000000000007
Hull 0.8362012717721877
Middlesbrough 0.6971943443304693

England Premier League 2017/2018
=====
Manchester City 2.398823204419891
Liverpool 1.871100993377485
Tottenham 1.8331631244824735
Arsenal 1.6883651452282165
Manchester Utd 1.5726460005535572
Chelsea 1.4510011061946915
Crystal Palace 1.403015741507872
Leicester 1.2518565517241396
Watford 1.1562657534246574
Everton 1.1204689655172415
Newcastle 1.0640897755610998
West Ham 1.0446826051112954
Bournemouth 0.9957362637362651
Brighton 0.9839266870313802
Southampton 0.9228472987872113
Stoke 0.8937382661512978
Burnley 0.8835910224438907
West Brom 0.8344257316399778
Swansea 0.7753942254303168
Huddersfield 0.7536753318584073Pandas dataframe usage examplefromxgclient.clientimportExpectedGoalsClient,create_fixtures_dataframe,create_events_dataframe,create_fixture_oddsclient=ExpectedGoalsClient('Your API Key')season_fixtures=client.fixtures(8202)fixtures_df=create_fixtures_dataframe(season_fixtures)events_df=create_events_dataframe(season_fixtures)upcoming_odds_df=create_fixture_odds(client.upcoming_odds())