A Python module to scrape data from basketbal-reference.com and convert it to pandas data structures for analysis.
Last updated Jun 10, 2026
24
Stars
7
Forks
3
Issues
0
Stars/day
Attention Score
12
Language breakdown
No language data available.
▸ Files
click to expand
README
PandasBasketball
PandasBasketball is a small module intended to scrape data from basketball-reference and convert it to useful pandas data structures, such as data frames, for future analytical purposes. The use of jupyter notebooks is encouraged.Installation
pip install PandasBasketball
After installation you can then import it to your environment like this:
from PandasBasketball import pandasbasketball as pb
Requirements
Please make sure you meet the following rquirements:- Python 3.6+
- requests
- pandas
- Beautiful Soup 4
Usage
:basketball: Players
Inside a player's page on the basketball-reference website you can find several tables, and most of these tables can be obtained as a pandas data frame by callingget_player(player, stat). The 'player' refers to the name of the html file used by basketball-reference inside the url, and the 'stat' means the type of table.
The currently supported tables are:
- Per Game (
per_game) - Totals (
totals) - Per 36 Minutes (
per_minute) - Per 100 Poss (
per_poss) - Advanced (
advanced) - Playoffs Per Game (
playoffspergame) - Playoffs Totals (
playoffs_totals) - Playoffs Per 36 Minutes (
playoffsperminute) - Playoffs Per 100 Poss (
playoffsperposs) - Playoffs Advanced (
playoffs_advanced)
Example
To get the 'Per Game' table for LeBron James you would do something like this:df = pb.getplayer("jamesle01", "pergame")
Optional Arguments
Theget_player() method supports two optional arguments:
numeric- returns the data frame with its columns alreay converted to numerics_index- returns the data frame with its column 'Season' as the index
False by deault.
Considerations
- The resulting data frame does not include the table's footer.
- The resulting data frame will have the same column names as the table's header but it will not have a set index. To set the 'Season' column as index set the argument
s_indextoTrue. - The columns will be of type 'object', so in order to perform arithmetic functions on them you will need to convert them to numeric. You can do something like this:
lbjpg = pb.getplayer("jamesle01", "per_game")
lbjpg[lbjpg.columns] = lbjpg[lbjpg.columns].apply(pd.to_numeric, errors="ignore")
Or you cant set the optional argument numeric to True.
:basketball: Player Game Logs
You can get all of a player's games in a season by callinggetplayergamelog(player, season). The season argument must be the last year in which the season took place.
Example
To get all of Kawhi Leonard's games during the 2017-2018 season:df = pb.getplayergamelog("leonaka01", "2018")
Optional Arguments
The functiongetplayergamelog supports one optional argument:
playoffs- returns only the playoffs games if set toTrue
False by default.
Considerations
- The resulting data frame will use the 'Rk' column as its index
- The data frame does not include those rows which are just the header again
- If the player missed a game, the row will be filled with blanks ("")
:basketball: Last n days
Get a data frame with all the season's available players stats over the last n days by callinggetndays(days).
Example
df = pb.getndays(10)
Optional arguments
getndays supports one optional argument:
player- returns a pandas series with the stats of the specifed player
player is set to all by default.
Considerations
- The resulting data frame will have the column 'Players' as its index by default
- The data frame is in descending order by GmSc
:basketball: Teams
You can call a team's seasons table withget_team(name). The argument name is the team's three-letter abbreviation (e.g. OKC, MAV).
Example
To get OKC's table:df = pb.get_team("OKC")
:basketball: Generate player code
Baskteball-reference uses a special code to build each player's unique html page. As of now, almost all functions inPandasBasketball expect that code to get the stats for the specified athlete. If you don't want to copy and paste the code from the URL into the function you can try calling pb.generate_code(player).
Note: this is not fully tested, so it is possible to get an incorrect code.
Example
To get the player code for LeBronJames:pb.generate_code("LeBron James")
This will output 'jamesle01'
Using it with other functions:
df = pb.getplayer(pb.generatecode("Donovan Mitchell"), "per_game")
Future
Make this project pip-installable- Add support for the rest of tables on a player's page
- Implement function to obtain team stats per season
Implement function to get the last n days stats- Implement function to obtain game results by date
Contributions & Known Issues
If you notice an issue or want to contribute open an issue over at the issues section.🔗 More in this category