Skip to main content

License Contact

Adding data about players on court in NBA games.

player_on_court package allows you to add to play-by-play data information about players who were on court at any given time.

Important: This package does not request play-by-play data from NBA website. You need to get them in advance, for example, using nba_api package.

https://github.com/swar/nba_api

How it work

Play-by-play NBA data contains information about each event in the game (throw, substitution, foul, etc.) and players who participated in it (PLAYER1_ID, PLAYER2_ID, PLAYER3_ID).

From this data, we get a list of players who were on court in this quarter. Then, we need to filter this list to 10 people who started quarter. This is done by analyzing substitutions in quarter.

I will soon describe a more complete mechanism for processing play-by-play data to obtain information about players on court in an article.

In package two main functions: adding_player_on_court and replace_id_on_name.

adding_player_on_court takes play-by-play data as input and returns it with 10 columns of the PLAYER_ID of players who were on court at each time.

replace_on_id_name allows you to replace PLAYER_ID with first and last name of player. This allows user to understand exactly which players were on court (few know PLAYER_ID all players in NBA),but it is not necessary to do this before calculations, because the player’s NAME_SURNAME is not unique, unlike PLAYER_ID.

Code example

>>> from nba_api.stats.endpoints import playbyplayv2
>>> import player_on_court.player_on_court as poc
>>>
>>> pbp = playbyplayv2.PlayByPlayV2(game_id="0022100001").play_by_play.get_data_frame()
>>> pbp_with_players = poc.adding_player_on_court(pbp)
>>> len(pbp_with_players.columns) - len(pbp.columns)
10
>>> players_id = list(pbp_with_players.iloc[0, 34:].reset_index(drop=True))
>>> print(players_id)
[201142, 1629651, 201933, 201935, 203925, 201572, 201950, 1628960, 203114, 203507]
>>> players_name = poc.replace_id_on_name(players_id)
>>> print(players_name)
['Kevin Durant', 'Nic Claxton', 'Blake Griffin', 'James Harden', 'Joe Harris',
'Brook Lopez', 'Jrue Holiday', 'Grayson Allen', 'Khris Middleton', 'Giannis Antetokounmpo']

You can also replace the PLAYER_ID with the player’s name in the entire data frame at once.

>>> cols = ["PLAYER1", "PLAYER2", "PLAYER3", "PLAYER4", "PLAYER5", "PLAYER6", "PLAYER7", "PLAYER8", "PLAYER9", "PLAYER10"]
>>> pbp_with_players.loc[:, cols] = pbp_with_players.loc[:, cols].apply(poc.replace_id_on_name, result_type="expand")

Metadata

Release files for player-on-court 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for player-on-court 0.2.1
File Size Uploaded
player_on_court-0.2.1.tar.gz 6.2 kB Details

Release files / player_on_court-0.2.1.tar.gz

Download URL player_on_court-0.2.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3c23ada7dbc43b361a5be004a2a254c923be83b552e6a5b901204821d8eae5e5
BLAKE2b-256 checksum
How to use checksums
8184a68652a14bc7a0657e9e58d73b78d9a712e6902f140d42affb1a14804681
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.2.1 This release

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page