Get every player stats for a given season


In [29]:
import goldsberry
import pandas as pd
import os.path
import csv
from goldsberry.team import *
pd.set_option("display.max_columns",50) # Change Pandas Display Options
goldsberry.__version__


Out[29]:
'0.8.0.1'

Define method that writes data into a csv


In [16]:
def write_player_game_log_csv(team_file, player_game_logs):    
    with open(team_file, 'w') as csvfile:
        fieldnames = ['AST','BLK','DREB','FG3A','FG3M','FG3_PCT','FGA','FGM', 'FG_PCT',
                      'FTA', 'FTM', 'FT_PCT', 'GAME_DATE', 'Game_ID', 'LEAGUE_ID', 'MATCHUP', 'MIN', 'OREB', 
                      'PF', 'PLUS_MINUS', 'PLAYER_ID', 'PTS', 'REB', 'SEASON_ID', 'STL',
                      'WL','TOV']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()        
        for team_row in player_game_logs.logs():
            writer.writerow({'AST' : team_row['AST'],
                              'BLK' : team_row['BLK'],
                              'DREB' : team_row['DREB'],
                              'FG3A' : team_row['FG3A'],
                              'FG3M' : team_row['FG3M'],
                              'FG3_PCT' : team_row['FG3_PCT'],
                              'FGA' : team_row['FGA'],
                              'FGM' : team_row['FGM'],
                              'FG_PCT' : team_row['FG_PCT'],
                              'FTA' : team_row['FTA'],
                              'FTM' : team_row['FTM'],
                              'FT_PCT' : team_row['FT_PCT'],
                              'GAME_DATE' : team_row['GAME_DATE'],
                              'Game_ID' : team_row['Game_ID'],
                              'MATCHUP' : team_row['MATCHUP'],
                              'MIN' : team_row['MIN'],
                              'OREB' : team_row['OREB'],
                              'PF' : team_row['PF'],
                              'PLUS_MINUS' : team_row['PLUS_MINUS'],
                              'PTS' : team_row['PTS'],
                              'PLAYER_ID' : team_row['Player_ID'],
                              'REB' : team_row['REB'],
                              'SEASON_ID' : team_row['SEASON_ID'],
                              'STL' : team_row['STL'],
                              'TOV' : team_row['TOV'],
                              'WL': team_row['WL']})

Get player list and game logs for each player


In [23]:
def get_player_stats(season='2015'):
    player_list = goldsberry.PlayerList()
    
    for player in player_list.players():
        player_name = player['PLAYERCODE']
        if player_name != None:
            csv_name = "%s_%s.csv" %(season, player_name)
            player_code = player['PERSON_ID']
            print csv_name, os.path.isfile(csv_name)

            if (os.path.isfile(csv_name) == False):
                player_game_logs = goldsberry.player.game_logs(player_code)
                write_player_game_log_csv(player_csv, player_game_logs)

In [30]:
get_player_stats()