This notebook demonstrates a game's data and analyse its various data
In [1]:
%load_ext autoreload
%autoreload 2
from statsnba import Game, Api
import requests_cache
import pandas as pd
pd.options.display.max_columns = 999
requests_cache.install_cache('test_cache')
In [2]:
api = Api()
game_ids = api.GetSeasonGameIDs('2009-10', 'Regular Season')
game_ids
sample_game_id = game_ids[0]
In [3]:
sample_game = Game(sample_game_id)
sample_game
Out[3]:
In [4]:
def matchup_to_df(matchups):
lst = []
for matchup in sample_game.Matchups:
matchup_dict = {}
_home_players = sorted([p.PlayerName for p in matchup.HomePlayers])
_away_players = sorted([p.PlayerName for p in matchup.AwayPlayers])
home_players = dict(zip(['h{}'.format(i) for i in range(5)], _home_players))
away_players = dict(zip(['a{}'.format(i) for i in range(5)], _away_players))
matchup_dict.update(home_players)
matchup_dict.update(away_players)
home_boxscore = matchup.Boxscore.HomeTeamStats
away_boxscore = matchup.Boxscore.AwayTeamStats
matchup_dict.update({'home_{}'.format(k):v for k,v in home_boxscore.items()})
matchup_dict.update({'away_{}'.format(k):v for k,v in away_boxscore.items()})
lst.append(matchup_dict)
return lst
df = pd.DataFrame(matchup_to_df(sample_game.Matchups))
print 'The first two rows of the matchups and their stats'
df.head(2)
Out[4]:
In [5]:
print "Description of the various statistics"
df.describe()
Out[5]: