Skip to content

Instantly share code, notes, and snippets.

@jmadden173
Created January 29, 2020 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmadden173/4fa69c88906d50f0fd8f75af6baf5d0e to your computer and use it in GitHub Desktop.
Save jmadden173/4fa69c88906d50f0fd8f75af6baf5d0e to your computer and use it in GitHub Desktop.
Gets matches but instead of getting the stats for the game it merges the team stats for that given year.
#!/usr/bin/env python
import pandas as pd
from nba_api.stats.endpoints import leaguegamefinder, teamyearbyyearstats
# Define what years to collect game data on
years = ["2015-16",
"2016-17",
"2017-18",
"2018-19",]
# Get dataframe of games
games = pd.DataFrame()
for y in years:
print("Getting games for {}... ".format(y), end='', flush=True)
g = leaguegamefinder.LeagueGameFinder(
player_or_team_abbreviation="T",
league_id_nullable="00",
season_type_nullable="Regular Season",
season_nullable=y,
)
print("Done!")
g_df = g.league_game_finder_results.get_data_frame()
g_df["YEAR"] = y
games = games.append(g_df)
# Filter out relevent columns
games = games[["GAME_ID", "TEAM_ID", "MATCHUP", "GAME_DATE", "WL", "YEAR"]]
# Find team ids
team_ids = list(g_df["TEAM_ID"].unique())
# Get team stat for all years
team_stats = pd.DataFrame()
for t in team_ids:
print("Getting team stats for {}... ".format(t), end='', flush=True)
stats = teamyearbyyearstats.TeamYearByYearStats(
league_id="00",
per_mode_simple="Totals",
season_type_all_star="Regular Season",
team_id=t,
)
print("Done!")
stats_df = stats.team_stats.get_data_frame()
team_stats = team_stats.append(stats_df)
# Merge the stats into vertical pd of games
games_vertical_stats = pd.merge(left=games, right=team_stats, how='inner', on=["TEAM_ID","YEAR"])
# Get the first and last match data
dup1 = games_vertical_stats.drop_duplicates('GAME_ID', 'first')
dup2 = games_vertical_stats.drop_duplicates('GAME_ID', 'last')
# Drop columns that describe the same data
dup2 = dup2.drop(["MATCHUP"], axis=1)
dup2 = dup2.drop(["WL"], axis=1)
# Merge vertical stats of the two games
game_team_stats = pd.merge(dup1, dup2, on="GAME_ID")
print("\n")
print(game_team_stats.head())
print()
print(game_team_stats.columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment