Loading NBA Data in Python

Loading NBA CSV data from GitHub in Python.

Loading CSV Data

In the previous post, we covered calculating NBA Pythagorean wins in Python using NBA data containing team record, points scored, and points allowed. Improving upon the previous implementation, rather than explicitly typing one set of team stats at a time into the Python program, let's instead load a CSV file with the team data. Then, run the Pythagorean wins computation and output for all 30 NBA teams.

In Python there are multiple ways to load CSV file. The code snippet below fetches a CSV file from Github using the requests library, a popular package freely available on PyPi and recommended by the standard Python docs for a high-level HTTP client. After the CSV content is downloaded, the internal Python packages io and csv are used to convert the data into a list of dictionaries.

import csv
import io
import requests

# Download the csv file from Github
csv_url = 'https://raw.githubusercontent.com/kyleaclark/kyleaclark-data/main/basketball/nba-team-data-01-15-2022.csv'
csv_content = requests.get(csv_url).content

# Read the csv content and convert to a list of dictionaries
csv_reader = csv.DictReader(io.StringIO(csv_content.decode('utf-8')))
team_data = list(csv_reader)

For this tutorial there are technical limitations with using the requests library in a web browser (Python, like most programming languages, is not native to a web browser). As a workaround, the Pyodide library can be used to fetch the contents of the CSV file.

import csv
import pyodide

# Download the csv file from Github
csv_url = 'https://raw.githubusercontent.com/kyleaclark/kyleaclark-data/main/basketball/nba-team-summary-01-22-2022.html'
csv_stream = pyodide.open_url(csv_url)

# Read the csv text strem and convert to a list of dictionaries
csv_reader = csv.DictReader(csv_stream)
team_data = list(csv_reader)

In your local code, not on a web browser, use requests or an alternative library as an HTTP client.

Live Example

Putting all the pieces together. There are a few changes within this program to improve the structure and load the CSV data stored on GitHub. The steps follow the basic order of load team data, loop through the list of team data, then compute the Pythagorean wins, and lastly output the stats summary per team.

Walk through the code. Learn how to load a CSV file in Python within the context of a real program. For experimentation, change the output summary format or add new stats to display, like a team's current win percentage. Run the code to see the results.

Loading...
Published