# calculate time elapsed between a free throw and whatever action came before itĭf = df.sort_values(by=)ĭf = df.astype('datetime64')ĭf = (df - df).astype('timedelta64') # get data from all ids (takes awhile)ĭf = pd.concat(pbpdata, ignore_index=True) Note that these two steps (particularly the first one) take a few minutes to run and if you don’t want to wait that long, just download the cleaned data I already uploaded to GitHub. After that, we’re going to find all the instances of free throw attempts in our data and calculate how much time elapsed from when the player attempted their free throw from whatever action that preceded the free throw. Return df Now we just need to run every game ID through this function and store the results in one giant dataframe. Response = requests.get(url=play_by_play_url, headers=headers).json() # create function that gets pbp logs from the 2020-21 season Gamefinder = leaguegamefinder.LeagueGameFinder(season_nullable='2020-21', So lets get all the game IDs and then create our function for getting the play-by-play data. But first, we need to specify some headers in advance so that when we make a request to the NBA’s API we don’t get timed out ( h/t Ryan Davis ). To get the data in more traditional format, like a dataframe, we just need to run a few lines of code in Python. The data, like most stuff on, is stored in JSON format. Here’s an example of the data, which is the live play-by-play data for Game 4 of the Finals between the Bucks and Suns. import requestsįrom nba_ import leaguegamefinderįrom joypy import joyplot The data we’re going to work comes from the NBA’s live play-by-play logs (this is a different data source than the traditional play-by-play logs that we’ve worked with in the past). Let’s start off by loading some packages and setting up a custom theme that we’ll use for our chart. I’ll also spend some time going over the code for the accompanying visualization. Today’s tutorial is going to go over how I got the data on timestamps during NBA games to measure how long Giannis Antetokounmpo spends at the free throw line relative to the rest of the league. You can check out my code on GitHub here. It appears R is far ahead of Python in this area. The only major hiccup is the ridgeline plot at the end. We’re going to take one of his recent posts and translate the R code to Python as best we can. But if you’re also like me, the R code may go over your head since you only know Python…Īlas, have no fear. If you’re like me, you’re super grateful to see any how-to post related to NBA data. The best part is, he walks through how he creates these visualizations by posting R tutorials. He shares charts and visualizations of NBA-related data each week. let teamRecordRequest = playByPlay.Translating Owen Phillips Code from R to Pythonįor those NBA Data fans that are not subscribed to Owen Phillips F5 newsletter, stop what you’re doing and check it out. This returns a Promise that will resolves to a TeamRecord object containing game entries for a team's season(s). Getting teamId: console.log(playByPlay.getTeamId("Warriors")) Ĭonsole.log(playByPlay.getTeamId("GSW")) Usage const playByPlay = require('nba-play-by-play') Installation npm install -save nba-play-by-play A simple, lightweight utility API to grab NBA play-by-play data to facilitate gathering data not reflected in boxscores.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |