Jchua6
Jchua6

Reputation: 11

Only call a certain number of rows from an API

Im collecting game data for recent nba games played by every team, I wanted to know if there was a way to only collect the most recent 12 games played instead of grabbing all the games. Something similar to df.head(12). I have posted my code below and included a screenshot as well.

Thanks

from nba_api.stats.endpoints import teamgamelog
BGL = teamgamelog.TeamGameLog(team_id ='1610612738')
print(BGL.team_game_log.get_data_frame())

Upvotes: 1

Views: 132

Answers (2)

chitown88
chitown88

Reputation: 28565

Convert your 'GAME_DATE' column to datetime. Then sort your dataframe by the 'GAME_DATE' column. Then grab the top 12 rows with .head(12) (or bottom 12 if your sorted in ascending order with .tail(12))

import pandas as pd
from nba_api.stats.endpoints import teamgamelog


BGL = teamgamelog.TeamGameLog(team_id ='1610612738')
df = BGL.team_game_log.get_data_frame()

# convert the 'GAME_DATE' column to datetime format 
df['GAME_DATE']= pd.to_datetime(df['GAME_DATE']) 

# sort dataframe by date (most recent -> )
df = df.sort_values(by='GAME_DATE', ascending=False).reset_index(drop=True)

# just get x most recent games
numGames = 12
df_filter = df.head(numGames)


print(df_filter)

Output:

       Team_ID     Game_ID  GAME_DATE      MATCHUP WL   W  L  W_PCT  MIN  FGM  FGA  FG_PCT  FG3M  FG3A  FG3_PCT  FTM  FTA  FT_PCT  OREB  DREB  REB  AST  STL  BLK  TOV  PF  PTS
0   1610612738  0021900529 2020-01-04    BOS @ CHI  W  25  8  0.758  240   43   82   0.524     8    19    0.421   17   21   0.810    13    31   44   22   10    5   19  22  111
1   1610612738  0021900517 2020-01-03  BOS vs. ATL  W  24  8  0.750  240   41   93   0.441     6    25    0.240   21   29   0.724    12    42   54   23    9    7   12  16  109
2   1610612738  0021900497 2019-12-31    BOS @ CHA  W  23  8  0.742  240   44   97   0.454    14    38    0.368    7   10   0.700    15    39   54   25   12    9   10  16  109
3   1610612738  0021900473 2019-12-28  BOS vs. TOR  L  22  8  0.733  240   31   78   0.397     7    32    0.219   28   36   0.778     7    24   31   18   11    4   14  17   97
4   1610612738  0021900466 2019-12-27  BOS vs. CLE  W  22  7  0.759  240   49   95   0.516    14    40    0.350   17   19   0.895    12    31   43   28    4   10   11  23  129
5   1610612738  0021900455 2019-12-25    BOS @ TOR  W  21  7  0.750  240   46   92   0.500    14    33    0.424   12   14   0.857    13    32   45   26    7    3   19  22  118
6   1610612738  0021900440 2019-12-22  BOS vs. CHA  W  20  7  0.741  240   46   88   0.523    14    32    0.438   13   16   0.813    10    47   57   25    2    9   11  24  119
7   1610612738  0021900422 2019-12-20  BOS vs. DET  W  19  7  0.731  240   45   86   0.523     9    27    0.333   15   21   0.714    13    38   51   24   13    9   20  19  114
8   1610612738  0021900413 2019-12-18    BOS @ DAL  W  18  7  0.720  240   35   86   0.407    12    38    0.316   27   30   0.900    14    40   54   13    3    4   15  21  109
9   1610612738  0021900367 2019-12-12  BOS vs. PHI  L  17  7  0.708  240   42   91   0.462    11    31    0.355   14   17   0.824     9    24   33   24    4    7    6  25  109
10  1610612738  0021900357 2019-12-11    BOS @ IND  L  17  6  0.739  240   43   87   0.494    14    36    0.389   17   22   0.773     8    36   44   25    6    4   15  27  117
11  1610612738  0021900344 2019-12-09  BOS vs. CLE  W  17  5  0.773  240   45   78   0.577    15    27    0.556    5    8   0.625     5    34   39   22    8    9   18  19  110

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71560

Use groupby.head:

print(BGL.team_game_log.get_data_frame().groupby('Team_ID').head(12))

Upvotes: 1

Related Questions