seemethere / nba_py

Python client for NBA statistics located at stats.nba.com
BSD 3-Clause "New" or "Revised" License
1.05k stars 256 forks source link

Get advanced stats for a player by Game #101

Closed camrongodbout closed 6 years ago

camrongodbout commented 6 years ago

Using: player.PlayerGameLogs(2544).info() you can get all the stats for an individual player by game.

I would like to get the advanced stats found in the snippet below to be by game as well player.PlayerGeneralSplits(2544, measure_type='Advanced')

Any ideas?

ryant030408 commented 6 years ago

Well that is splitting home vs. away games so it is lumping them together. If you would like all of 2544("LeBron James") advanced stats by game you would get the advanced boxscore from the game module and have to grab every game he played in.

Here's some code i wrote, this will grab every game in the year supplied in console. All data is saved in pickles to be retrieved whenever you would like with pandas. It takes a while to run but will get you a whole seasons advanced stats for every game.

This only works with seasons with 1230 games, I haven't bothered making it better because it served it purpose.

import nba_py, sys, time
from nba_py import game
import pickle
import pandas as pd

year = sys.argv[1]

counter = 0
boxscore = game.BoxscoreAdvanced('00' + year +'00001')
boxscore_data = boxscore.sql_players_advanced()
for i in range(2, 1231):
    if((counter%10) == 0):
       boxscore_data.to_pickle('players_advanced_for_' + year + '.pckl')
    boxscore_data = boxscore_data.append(game.BoxscoreAdvanced("00" + year + "0" + str(i).zfill(4)).sql_players_advanced())
    print(i)
    counter+=1 
    time.sleep(2)

boxscore_data.to_pickle('players_advanced_for_' + year + '.pckl') 
camrongodbout commented 6 years ago

@ryant030408 Thank you. That was what I was looking for.

Wasn't aware of the sql_players_advanced() method.