def df_api_lideres(temporada, tipo): response = leagueleaders.LeagueLeaders(season=temporada, season_type_all_star=tipo) df = response.get_data_frames()[0] #df.head(10) df1 = df[['PLAYER_ID', 'RANK', 'PLAYER', 'TEAM', 'AST', 'PTS']] df1 = df1.head(20) return df1
def league_leaders(): leaders = leagueleaders.LeagueLeaders() leaders_df = leaders.get_data_frames()[0] ranking_df = leaders_df[['PLAYER', 'RANK']] #LeagueLeader rank added to df ll_joined = pd.merge(df, ranking_df, left_on=['full_name'], right_on=['PLAYER']) ll_joined.to_csv('NBARANK.csv')
def perGame(): data = leagueleaders.LeagueLeaders() df = data.league_leaders.get_data_frame() was, stats = df[df.TEAM == 'WAS'], ['PTS', 'REB', 'AST', 'STL'] wash_per = was[stats].div(was['GP'], axis=0).round(1) wash_per = pd.concat([was['PLAYER'], wash_per], axis=1) # wash_per.PLAYER = wash_per.PLAYER.str.split(n=1).str[1] wash_per = wash_per.set_index('PLAYER') return tabulate(wash_per, tablefmt='pipe', headers='keys', colalign=("left", "center", "center", "center", "center"), floatfmt=".1f")
def getLeagueLeaderInfo(self, league_num="00", mode="PerGame", my_scope="S", my_season="2018-19", my_type="Regular Season", category="PTS"): leader_info = leagueleaders.LeagueLeaders( league_id=league_num, per_mode48=mode, scope=my_scope, season=my_season, season_type_all_star=my_type, stat_category_abbreviation=category) with open('nba.json', 'w') as fp: json.dump(leader_info.get_dict(), fp, indent=3) return leader_info.get_dict()
#this file uses the nba_api to create and then concatenate dataframes of each seasons' league leaders for major statistics only #import endpoints and necessary parameters for the api from nba_api.stats.endpoints import leagueleaders from nba_api.stats.library.parameters import PerMode48 import pandas as pd #reads in seasons and formats as a list seasons_df = pd.read_excel(io='years_nba.xlsx') seasons_list = [row['seasons'] for index, row in seasons_df.iterrows()] #obtain most recent year's league leaders data as pergame averages data league_leaders_upto_2020_raw = leagueleaders.LeagueLeaders( per_mode48=PerMode48.per_game, season=seasons_list[0]) stats_upto_2020_df = league_leaders_upto_2020_raw.get_data_frames()[0] #add column of year num_rows = stats_upto_2020_df.shape[0] year = [seasons_list[0]] * num_rows stats_upto_2020_df['Year'] = year #obtain each years' data, format as dataframe, and then concatenate with original dataframe for nba_season in seasons_list: if nba_season == seasons_list[0]: continue stats_raw = leagueleaders.LeagueLeaders(per_mode48=PerMode48.per_game, season=nba_season) stats_df = stats_raw.get_data_frames()[0] num_rows = stats_df.shape[0] year = [nba_season] * num_rows stats_df['Year'] = year
from nba_api.stats.endpoints import commonteamroster import json roster_info = commonteamroster.CommonTeamRoster(season=2017, team_id=1610612739) roster_cavs = roster_info.get_dict() with open('nba.json', 'w') as fp: json.dump(roster_cavs, fp, indent=3) a = roster_cavs['resultSets'] b = a[0] c = b['rowSet'] for player in c: print(player[3]) from nba_api.stats.endpoints import leagueleaders league_info = leagueleaders.LeagueLeaders( league_id="00", per_mode48="PerGame", scope="S", season="2018-19", season_type_all_star="Regular Season", stat_category_abbreviation="PTS") print(league_info.get_dict()) with open('nba.json', 'w') as fp: json.dump(league_info.get_dict(), fp, indent=3)
player_or_team="Player", pt_measure_type="SpeedDistance") stats_drives = leaguedashptstats.LeagueDashPtStats(player_or_team="Player", pt_measure_type="Drives") stats_catchshoot = leaguedashptstats.LeagueDashPtStats( player_or_team="Player", pt_measure_type="CatchShoot") stats_pullup = leaguedashptstats.LeagueDashPtStats( player_or_team="Player", pt_measure_type="PullUpShot") stats_postup = leaguedashptstats.LeagueDashPtStats(player_or_team="Player", pt_measure_type="PostTouch") stats_passing = leaguedashptstats.LeagueDashPtStats(player_or_team="Player", pt_measure_type="Passing") stats_possession = leaguedashptstats.LeagueDashPtStats( player_or_team="Player", pt_measure_type="Possessions") stats_rebounding = leaguedashptstats.LeagueDashPtStats( player_or_team="Player", pt_measure_type="Rebounding") stats_leagueleaders = leagueleaders.LeagueLeaders() player_ids = set() #[player['id'] for player in players.get_active_players()] player_ids_ignore = set() # Heights and weights were causing issues trying to connect to the commonplayerinfo endpoint, so they have been omitted #player_heights = [] #player_weights = [] #for player_id in player_ids: # try: # height_string = commonplayerinfo.CommonPlayerInfo(player_id=player_id).common_player_info.data['data'][0][10].split('-') # if len(height_string) is 2: # player_heights.append(int(height_string[0]) * 12 + int(height_string[1])) # convert height to inches # else: #No data available # player_ids_ignore.append(player_id) # except:#connection error # player_ids_ignore.append(player_id)
# fetchdata.py # Gets league leader statistics from NBA-API and writes to text file "stats.txt" import pandas as pd from nba_api.stats.endpoints import leagueleaders # importing league leaders lleaders2020 = leagueleaders.LeagueLeaders(league_id="00") lleaders_df = lleaders2020.get_data_frames()[0] # creating data frame #sort stats counter = 0 # count amount of players in list PlayerList = [] for index, row in lleaders_df.iterrows(): # iterate through dataframe statlist = [] counter += 1 if counter <= 40: PPG = round(row["PTS"] / row["GP"], 1) # calculate stat/game RPG = round(row["REB"] / row["GP"], 1) APG = round(row["AST"] / row["GP"], 1) BPG = round(row["BLK"] / row["GP"], 1) SPG = round(row["STL"] / row["GP"], 1) statlist.append(row["PLAYER"]) statlist.append(row["TEAM"]) statlist.append(PPG) # append to list statlist.append(RPG) statlist.append(APG) statlist.append(BPG) statlist.append(SPG) PlayerList.append(statlist) #write to file stats = open("stats.txt", "w") # open file for p in PlayerList: