swar / nba_api

An API Client package to access the APIs for NBA.com
MIT License
2.34k stars 515 forks source link

Game_id #432

Closed leonv79 closed 3 months ago

leonv79 commented 3 months ago

Hi! I'm kinda new here so i'm a bit unfamiliar with the package. First of all the work that has been done is terrific. Really useful stuff. Now to my problem. I see that you can find player and team ids pretty easy on the static folder, but what about the game_ids? I want to gather all stats for a season in boxscorehustlev2 and it only requires game_ids. Any help would be really useful. Thanks in advance!

pfredCL commented 3 months ago
from nba_api.stats.static import teams
from nba_api.stats.endpoints import leaguegamefinder
import pandas as pd

list_of_games = []

nba_teams = teams.get_teams()
team_ids = [team['id'] for team in nba_teams]
for id in team_ids:
    gamefinder = leaguegamefinder.LeagueGameFinder(team_id_nullable=id)
    games = gamefinder.get_data_frames()[0]
    list_of_games.append(games)

final_df = pd.concat(list_of_games, ignore_index=True)
game_ids = list(set(final_df['GAME_ID']))

This will get you basically every game that the API has available. You can further filter the dataframe by date, team, etc. Enjoy!

leonv79 commented 3 months ago

You saved me so much time, thanks a lot!