简体   繁体   中英

NBA API: Looping a dictionary to get data frames

I am trying to get a full database of all active players career stats in the NBA.

I'm relatively new to Python and am trying to figure out a way to iterate a loop function by looking up the playerID for each PlayerCareerStat data frame. Ultimately I will summarize and group the data so its easier to read but I am trying to return a list of all players career stats by season.

I am able to use the players.get_active_players() endpoint to return a list of all players with their player_id: [1]: https://i.stack.imgur.com/BFxgv.png

With that, I am tryin to loop the Player_id through each data frame in the PlayerCareerStats() endpoint... I think? Since the parameter for this endpoint requires a single player_id I can't seem to get all the players. Please see picture [1]: https://i.stack.imgur.com/skM8Y.png

Does anyone know how I might be able to get the output I am trying to find?

When you create a function, it will end when it reaches return . So your function returns the player id, and doesn't move on to the part where you are trying to pull the career stats.

So get rid of that return player['id'] , and the function should run all the way through:

from nba_api.stats.static import players
from nba_api.stats.endpoints import playercareerstats
import pandas as pd


def get_nba_id():
    nba_players = players.get_active_players()
    
    for player in nba_players:
        career_stats = playercareerstats.PlayerCareerStats(player_id=player['id'])
        career_df = career_stats.get_data_frames()[0]
        career_df['NAME'] = player['full_name']
        print(career_df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM