nba-api

1.11.4 · active · verified Thu Apr 16

nba-api is a free and active Python client package designed to access statistics from the Official NBA stats page (nba.com). It provides a robust interface for developers to retrieve data on NBA teams, seasons, players, and games in various formats, including pandas DataFrames. The library is actively maintained with frequent updates to adapt to changes in the underlying NBA.com API.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch a player's ID using the static module and then retrieve their career statistics from an endpoint. It shows how to access the data as a pandas DataFrame, which is a common and convenient way to work with the results.

from nba_api.stats.static import players
from nba_api.stats.endpoints import playercareerstats

# Find LeBron James' player ID
nba_players = players.get_players()
lebron = [player for player in nba_players if player['full_name'] == 'LeBron James'][0]
lebron_id = lebron['id']

# Get LeBron James' career stats
career_stats = playercareerstats.PlayerCareerStats(player_id=lebron_id)

# Access data as a pandas DataFrame (requires pandas installed)
career_df = career_stats.get_data_frames()[0]
print(f"LeBron James' Career Stats (first 5 rows):\n{career_df.head()}")

# Access data as a JSON dictionary
career_json = career_stats.get_json()
# print(f"\nLeBron James' Career Stats (JSON excerpt):\n{career_json[:200]}...")

view raw JSON →