0
from nba_api.stats.endpoints import commonplayerinfo, leagueleaders
import pandas as pd

# Function to retrieve data from the NBA API and convert it to a DataFrame
def get_data_from_api(endpoint, params):
    data = endpoint(**params)
    headers = data['resultSets'][0]['headers']
    rows = data['resultSets'][0]['rowSet']
    df = pd.DataFrame(rows, columns=headers)
    return df

# 1. Style of defense over time
defense_params = {
    'LeagueID': '00',
    'Season': '2021-22',
    'PerMode': 'PerGame',
    'MeasureType': 'Defense'
}
defense_data = get_data_from_api(leagueleaders.LeagueLeaders, defense_params)

# Get the player IDs from the defense_data DataFrame
player_ids = defense_data['PLAYER_ID'].unique().tolist()

# 2. Player height and body composition
player_data = pd.DataFrame(columns=['PERSON_ID', 'HEIGHT', 'WEIGHT'])
for player_id in player_ids:
    player_params = {
        'PlayerID': player_id
    }
    player_info = get_data_from_api(commonplayerinfo.CommonPlayerInfo, player_params)
    player_info = player_info[['PERSON_ID', 'HEIGHT', 'WEIGHT']]
    player_data = player_data.append(player_info, ignore_index=True)

# 3. Player age and playing time
age_params = {
    'LeagueID': '00',
    'Season': '2021-22',
    'PerMode': 'PerGame',
    'MeasureType': 'Advanced'
}
age_data = get_data_from_api(leagueleaders.LeagueLeaders, age_params)

# 4. Trends in three-point shooting
three_point_params = {
    'LeagueID': '00',
    'Season': '2021-22',
    'PerMode': 'PerGame',
    'MeasureType': 'Base'
}
three_point_data = get_data_from_api(leagueleaders.LeagueLeaders, three_point_params)


# 6. Shooting efficiency over time
shooting_efficiency_params = {
    'LeagueID': '00',
    'Season': '2021-22',
    'PerMode': 'PerGame',
    'MeasureType': 'Base'
}
shooting_efficiency_data = get_data_from_api(leagueleaders.LeagueLeaders, shooting_efficiency_params)


TypeError: LeagueLeaders.init() got an unexpected keyword argument 'LeagueID'

I keep getting this error. All i want is to extract some stats from some NBA seasons. I am working on creating visualizations understanding how the game has evolved overtime. How can i get this data using api pacakge?

Awadis
  • 1

0 Answers0