In [2]:
import nba_py
import datetime
import time
import nba_py.game
import nba_py.player
import nba_py.team
import pandas as pd
import numpy as np
import functools
import time
%matplotlib inline

today = datetime.date.today()
tomorrow = today + datetime.timedelta(days = 1)
someday = datetime.date(2017, 1, 1)

complete functions


In [2]:
def get_games(date):
    '''
    return a pandas.Series of all the games available on this day.
    '''
    return nba_py.Scoreboard(month = date.month, 
                             day = date.day, 
                             year = date.year, 
                             league_id = '00', 
                             offset = 0).game_header()[['GAME_ID', 'HOME_TEAM_ID', 'VISITOR_TEAM_ID']]

games = get_games(someday)
games


Out[2]:
GAME_ID HOME_TEAM_ID VISITOR_TEAM_ID
0 0021600508 1610612737 1610612759
1 0021600509 1610612748 1610612765
2 0021600510 1610612754 1610612753
3 0021600511 1610612750 1610612757
4 0021600512 1610612747 1610612761

In [ ]:
def get_teams(games):
    '''
    return a pandas.DataFrame including every games' home team id and away team id.
    '''
    teams = [nba_py.game.BoxscoreSummary(g).game_summary().loc[0, ['HOME_TEAM_ID', 'VISITOR_TEAM_ID']] 
             for g in games]
    return pd.DataFrame(teams, index = [g for g in games])

teams = get_teams(games)
teams

In [30]:
all_players = nba_py.player.PlayerList(season = '2017-18').info()

def get_players_new(games):
    home_team_player = all_players[all_players['TEAM_ID'].isin(games['HOME_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
    home_team_player['Location'] = 'HOME'
    away_team_player = all_players[all_players['TEAM_ID'].isin(games['VISITOR_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
    away_team_player['Location'] = 'AWAY'
    players = pd.concat([home_team_player, away_team_player])
    game_team = pd.concat([games[['HOME_TEAM_ID', 'GAME_ID']].rename(columns = {'HOME_TEAM_ID': 'TEAM_ID'}), 
                           games[['VISITOR_TEAM_ID', 'GAME_ID']].rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID'})])
    players = pd.merge(players, game_team, on = 'TEAM_ID')
    team_team = pd.concat([games[['HOME_TEAM_ID', 'VISITOR_TEAM_ID']].\
                           rename(columns = {'HOME_TEAM_ID': 'TEAM_ID', 'VISITOR_TEAM_ID': 'Against_Team_ID'}), 
                           games[['VISITOR_TEAM_ID', 'HOME_TEAM_ID']].\
                           rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID', 'HOME_TEAM_ID': 'Against_Team_ID'})])
    return pd.merge(players, team_team, on = 'TEAM_ID')
    
    

players = get_players_new(games)
players


Out[30]:
PERSON_ID TEAM_ID Location GAME_ID Against_Team_ID
0 1628389 1610612748 HOME 0021600509 1610612765
1 201609 1610612748 HOME 0021600509 1610612765
2 201961 1610612748 HOME 0021600509 1610612765
3 1627773 1610612748 HOME 0021600509 1610612765
4 2617 1610612748 HOME 0021600509 1610612765
5 201949 1610612748 HOME 0021600509 1610612765
6 204020 1610612748 HOME 0021600509 1610612765
7 203585 1610612748 HOME 0021600509 1610612765
8 1626175 1610612748 HOME 0021600509 1610612765
9 203482 1610612748 HOME 0021600509 1610612765
10 1626196 1610612748 HOME 0021600509 1610612765
11 203079 1610612748 HOME 0021600509 1610612765
12 1628476 1610612748 HOME 0021600509 1610612765
13 1627855 1610612748 HOME 0021600509 1610612765
14 202355 1610612748 HOME 0021600509 1610612765
15 1628475 1610612748 HOME 0021600509 1610612765
16 1626159 1610612748 HOME 0021600509 1610612765
17 202332 1610612750 HOME 0021600511 1610612757
18 202357 1610612750 HOME 0021600511 1610612757
19 202710 1610612750 HOME 0021600511 1610612757
20 2037 1610612750 HOME 0021600511 1610612757
21 203476 1610612750 HOME 0021600511 1610612757
22 201959 1610612750 HOME 0021600511 1610612757
23 1626145 1610612750 HOME 0021600511 1610612757
24 203498 1610612750 HOME 0021600511 1610612757
25 1628383 1610612750 HOME 0021600511 1610612757
26 201575 1610612750 HOME 0021600511 1610612757
27 201952 1610612750 HOME 0021600511 1610612757
28 1626157 1610612750 HOME 0021600511 1610612757
29 203952 1610612750 HOME 0021600511 1610612757
... ... ... ... ... ...
143 200768 1610612761 AWAY 0021600512 1610612747
144 203909 1610612761 AWAY 0021600512 1610612747
145 1628035 1610612761 AWAY 0021600512 1610612747
146 1628452 1610612761 AWAY 0021600512 1610612747
147 101139 1610612761 AWAY 0021600512 1610612747
148 1626259 1610612761 AWAY 0021600512 1610612747
149 203512 1610612761 AWAY 0021600512 1610612747
150 1627751 1610612761 AWAY 0021600512 1610612747
151 1626181 1610612761 AWAY 0021600512 1610612747
152 1627783 1610612761 AWAY 0021600512 1610612747
153 202685 1610612761 AWAY 0021600512 1610612747
154 1627832 1610612761 AWAY 0021600512 1610612747
155 1627787 1610612761 AWAY 0021600512 1610612747
156 1626153 1610612761 AWAY 0021600512 1610612747
157 202340 1610612765 AWAY 0021600509 1610612748
158 203493 1610612765 AWAY 0021600509 1610612748
159 203083 1610612765 AWAY 0021600509 1610612748
160 1627740 1610612765 AWAY 0021600509 1610612748
161 204038 1610612765 AWAY 0021600509 1610612748
162 202699 1610612765 AWAY 0021600509 1610612748
163 202704 1610612765 AWAY 0021600509 1610612748
164 1626169 1610612765 AWAY 0021600509 1610612748
165 1628379 1610612765 AWAY 0021600509 1610612748
166 202720 1610612765 AWAY 0021600509 1610612748
167 1626246 1610612765 AWAY 0021600509 1610612748
168 1626242 1610612765 AWAY 0021600509 1610612748
169 203961 1610612765 AWAY 0021600509 1610612748
170 202397 1610612765 AWAY 0021600509 1610612748
171 201229 1610612765 AWAY 0021600509 1610612748
172 2757 1610612765 AWAY 0021600509 1610612748

173 rows × 5 columns


In [4]:
def get_players(teams):
    '''
    return a pandas.DataFrame of all the teams' players.
    teams must be a pandas.DataFrame created by the function get_teams().
    '''
    player_list = nba_py.player.PlayerList().info()
    all_teams = teams['HOME_TEAM_ID'].append(teams['VISITOR_TEAM_ID'])
    players = functools.reduce(pd.Series.append, 
                                [nba_py.team.TeamPlayers(team).season_totals()['PLAYER_ID'] 
                                 for team in all_teams])
    players = players.to_frame()
    players['TEAM_ID'] = [int(player_list[player_list['PERSON_ID'] == p]['TEAM_ID']) 
                          for p in players['PLAYER_ID']]
    players = players[players['TEAM_ID'].isin(all_teams.values)]
    players['AGAINST_TEAM_ID'] = [int(teams[teams['HOME_TEAM_ID'] == t]['VISITOR_TEAM_ID'] 
                              if t in teams['HOME_TEAM_ID'].values 
                              else teams[teams['VISITOR_TEAM_ID'] == t]['HOME_TEAM_ID']) 
                          for t in players['TEAM_ID']]
    return players
    

get_players(teams)


Out[4]:
PLAYER_ID TEAM_ID AGAINST_TEAM_ID
0 201166 1610612754 1610612753
1 2744 1610612754 1610612753
2 101139 1610612754 1610612753
3 1627777 1610612754 1610612753
4 203922 1610612754 1610612753
5 201952 1610612754 1610612753
6 1626202 1610612754 1610612753
7 202338 1610612754 1610612753
8 202362 1610612754 1610612753
9 202730 1610612754 1610612753
10 101145 1610612754 1610612753
11 1626167 1610612754 1610612753
12 202331 1610612754 1610612753
13 1626176 1610612754 1610612753
15 201152 1610612754 1610612753
1 203079 1610612748 1610612765
2 201609 1610612748 1610612765
3 202355 1610612748 1610612765
4 201949 1610612748 1610612765
5 201177 1610612748 1610612765
6 1626196 1610612748 1610612765
7 1626159 1610612748 1610612765
8 202337 1610612748 1610612765
9 1627855 1610612748 1610612765
10 203585 1610612748 1610612765
11 204020 1610612748 1610612765
12 2617 1610612748 1610612765
13 201961 1610612748 1610612765
14 203186 1610612748 1610612765
0 1627761 1610612737 1610612759
... ... ... ...
0 202329 1610612757 1610612750
1 203459 1610612757 1610612750
2 203468 1610612757 1610612750
3 203081 1610612757 1610612750
4 202334 1610612757 1610612750
5 202323 1610612757 1610612750
6 1627774 1610612757 1610612750
7 203994 1610612757 1610612750
9 203090 1610612757 1610612750
10 203086 1610612757 1610612750
11 203943 1610612757 1610612750
12 1626192 1610612757 1610612750
13 203894 1610612757 1610612750
14 1627817 1610612757 1610612750
0 203998 1610612761 1610612747
1 202709 1610612761 1610612747
2 201942 1610612761 1610612747
3 201960 1610612761 1610612747
4 1626153 1610612761 1610612747
5 1627832 1610612761 1610612747
6 1627751 1610612761 1610612747
8 202685 1610612761 1610612747
9 200768 1610612761 1610612747
10 203512 1610612761 1610612747
11 1626181 1610612761 1610612747
12 200782 1610612761 1610612747
13 1627783 1610612761 1610612747
14 202335 1610612761 1610612747
15 201586 1610612761 1610612747
16 203082 1610612753 1610612754

151 rows × 3 columns


In [7]:
def get_last_n_game_logs(player_ID, game_ID, n):
    '''
    retrun a pandas.DataFrame of a player's last n game logs before a specific game.
    '''
    #game_logs = nba_py.player.PlayerGameLogs(player_ID).info()
    game_logs = game_logs_201939
    game_index = game_logs[game_logs['Game_ID']  < game_ID].index[:n]
    return game_logs.loc[game_index, ['MIN', 'PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK',
                                      'TOV', 'FGM', 'FGA', 'FG3M']]

last_n_game_201939 = get_last_n_game_logs('201939', '0021600437', 20)
last_n_game_201939


Out[7]:
MIN PTS AST OREB DREB STL BLK TOV FGM FGA FG3M
50 30 25 4 0 3 3 0 0 8 18 4
51 27 19 6 0 3 0 0 3 6 11 5
52 34 8 8 1 9 1 0 3 3 14 2
53 34 30 7 2 2 1 0 2 11 23 5
54 32 22 9 1 3 1 0 0 6 13 2
55 31 17 3 2 6 0 0 4 4 14 3
56 36 26 2 0 5 2 0 3 7 16 3
57 33 19 6 0 4 7 0 3 7 16 0
58 29 13 11 0 4 2 1 0 4 10 2
59 30 31 1 0 2 1 0 3 10 15 5
60 38 28 5 1 2 3 1 5 9 22 4
61 38 25 3 0 3 2 0 3 6 17 4
62 37 34 6 1 7 1 0 6 13 19 4
63 36 24 5 2 3 3 0 3 8 17 4
64 29 31 9 0 5 1 0 1 11 18 7
65 29 22 6 2 4 1 0 1 6 11 2
66 35 20 5 0 5 1 0 3 7 21 1
67 36 16 8 0 3 4 1 2 7 20 2
68 37 35 7 2 1 1 0 3 10 19 3
69 37 30 6 0 6 2 0 3 9 17 5

In [8]:
def get_score_36(game_logs):
    '''
    return the Fantasy score of the given game logs(pandas.DataFrame).
    '''
    convert_to_36 = lambda x: x[['PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK', 
                                 'TOV', 'FGM', 'FGA', 'FG3M']] * 36 / x['MIN']
    stats = game_logs.apply(convert_to_36, axis = 1).mean()
    score = stats['PTS'] * 1 + stats['AST'] * 1.5 + \
    stats['OREB'] * 1 + stats['DREB'] * 0.7 + \
    stats['STL'] * 2 + stats['BLK'] * 1.8 + stats['TOV'] * -1 + \
    stats['FGM'] * 0.4 + (stats['FGA'] - stats['FGM']) * -1 + stats['FG3M'] * 0.5
    return score

get_score_36(last_n_game_201939)


Out[8]:
36.180007640524536

In [9]:
def predict_score_36(player_ID, game_ID):
    '''
    return a list of last 20/10/5 games' average score.
    '''
    ma_20 = get_score_36(get_last_n_game_logs(player_ID, game_ID, 20))
    ma_10 = get_score_36(get_last_n_game_logs(player_ID, game_ID, 10))
    ma_5 = get_score_36(get_last_n_game_logs(player_ID, game_ID, 5))
    return [round(float(ma_20), 2), round(float(ma_10), 2), round(float(ma_5), 2)]

predict_score_36('201939', '0021601211')


Out[9]:
[40.67, 46.93, 49.66]

test


In [10]:
play_stats_cols = ['GROUP_VALUE', 'GROUP_SET', 'GP', 
                   'MIN', 'PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK', 'TOV', 
                   'FGM', 'FGA', 'FG3M']

In [11]:
def cal_score(stats):
    play_stats_cols = ['GROUP_VALUE', 'GROUP_SET', 'GP', 
                       'MIN', 'PTS', 'AST', 'OREB', 'DREB', 'STL', 'BLK', 'TOV', 
                       'FGM', 'FGA', 'FG3M']
    stats = stats[play_stats_cols].iloc[0]
    score = stats['PTS'] * 1 + stats['AST'] * 1.5 + \
    stats['OREB'] * 1 + stats['DREB'] * 0.7 + \
    stats['STL'] * 2 + stats['BLK'] * 1.8 + stats['TOV'] * -1 + \
    stats['FGM'] * 0.4 + (stats['FGA'] - stats['FGM']) * -1 + stats['FG3M'] * 0.5
    return score

round(float(cal_score(stats_201939)), 2)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-fba5048b71dc> in <module>()
      7     return score
      8 
----> 9 round(float(cal_score(stats_201939)), 2)

NameError: name 'stats_201939' is not defined

In [12]:
game_logs_201939 = nba_py.player.PlayerGameLogs('201939').info()
game_logs_201939[game_logs_201939['Game_ID'] == '0021601229'][['MIN', 'PTS', 'AST', 'OREB', 'DREB', 
                                                               'STL', 'BLK', 'TOV', 'FGM', 'FGA', 'FG3M']]


Out[12]:
MIN PTS AST OREB DREB STL BLK TOV FGM FGA FG3M
0 28 20 8 0 5 2 0 6 6 17 5

In [16]:
game_ID_list = game_logs_201939['Game_ID']
game_ID_list


Out[16]:
0     0021601229
1     0021601211
2     0021601171
3     0021601162
4     0021601152
5     0021601136
6     0021601118
7     0021601108
8     0021601094
9     0021601081
10    0021601055
11    0021601049
12    0021601035
13    0021601019
14    0021601003
15    0021600968
16    0021600959
17    0021600937
18    0021600928
19    0021600906
20    0021600889
21    0021600883
22    0021600873
23    0021600854
24    0021600847
25    0021600827
26    0021600813
27    0021600803
28    0021600792
29    0021600764
         ...    
49    0021600437
50    0021600427
51    0021600405
52    0021600388
53    0021600371
54    0021600358
55    0021600351
56    0021600335
57    0021600330
58    0021600314
59    0021600299
60    0021600281
61    0021600259
62    0021600244
63    0021600238
64    0021600223
65    0021600202
66    0021600191
67    0021600179
68    0021600165
69    0021600143
70    0021600119
71    0021600116
72    0021600099
73    0021600078
74    0021600069
75    0021600054
76    0021600036
77    0021600025
78    0021600003
Name: Game_ID, Length: 79, dtype: object

In [ ]:


In [82]:
prediction = []
for i in game_ID_list:
    stats = get_score_36(game_logs_201939[game_logs_201939['Game_ID'] == i]\
                                                    [['MIN', 'PTS', 'AST', 'OREB', 'DREB', 
                                                      'STL', 'BLK', 'TOV', 'FGM', 'FGA', 'FG3M']])
    ma = predict_score_36('201939', i)
    ma.append(round(float(stats), 2))
    prediction.append(ma)
    #time.sleep(2)
    
df = pd.DataFrame(prediction, columns = ['ma_20', 'ma_10', 'ma_5', 'comp'])[::-1]
df = df[1:]
df


Out[82]:
ma_20 ma_10 ma_5 comp
77 27.74 27.74 27.74 29.86
76 28.80 28.80 28.80 32.08
75 29.89 29.89 29.89 36.12
74 31.45 31.45 31.45 38.88
73 32.94 32.94 32.94 31.82
72 32.75 32.75 33.75 60.80
71 36.76 36.76 39.94 38.67
70 37.00 37.00 41.26 53.16
69 38.79 38.79 44.67 41.16
68 39.03 39.03 45.12 42.52
67 39.35 40.51 47.26 28.70
66 38.46 40.39 40.84 19.85
65 37.03 39.17 37.08 43.70
64 37.50 39.93 35.18 61.94
63 39.13 42.23 39.34 34.80
62 38.86 42.53 37.80 44.85
61 39.22 40.94 41.03 24.63
60 38.41 39.53 41.99 31.55
59 38.04 37.37 39.56 41.28
58 38.21 37.38 35.42 43.08
57 38.97 37.44 37.08 38.84
56 39.42 38.45 35.87 28.80
55 39.26 39.35 36.71 19.51
54 38.43 36.93 34.30 41.62
53 38.56 34.90 34.37 41.08
52 39.03 35.52 33.97 18.53
51 36.91 32.89 29.91 36.00
50 36.78 34.03 31.35 41.16
49 36.18 34.99 35.68 28.15
48 35.53 33.68 32.98 35.38
... ... ... ... ...
29 39.04 45.64 51.44 46.03
28 39.93 46.04 54.92 24.69
27 39.40 44.42 52.84 22.32
26 39.53 42.80 42.71 48.28
25 40.06 44.15 36.79 12.40
24 39.37 41.09 30.74 28.68
23 39.60 41.10 27.27 59.29
22 40.62 43.52 34.19 35.38
21 40.36 39.76 36.81 18.72
20 39.43 33.84 30.89 31.45
19 39.18 32.72 34.70 29.83
18 38.57 31.10 34.93 40.96
17 38.58 32.73 31.27 33.74
16 38.34 33.87 30.94 31.27
15 38.16 32.17 33.45 25.82
14 37.30 33.51 32.32 32.11
13 37.48 33.86 32.78 51.94
12 38.32 33.12 34.97 48.24
11 37.08 34.41 37.88 33.00
10 34.84 35.84 38.22 42.72
9 34.84 36.96 41.60 61.42
8 35.61 40.12 47.46 36.90
7 36.22 39.72 44.46 46.91
6 37.45 41.03 44.19 44.43
5 37.26 42.35 46.48 35.49
4 38.41 43.32 45.03 59.20
3 39.94 46.03 44.59 41.40
2 39.05 44.97 45.49 67.78
1 40.67 46.93 49.66 45.24
0 41.99 48.15 49.82 35.23

78 rows × 4 columns


In [83]:
mid = lambda x: x[['ma_20', 'ma_10', 'ma_5']].quantile(0.5)

df['wtd_avg'] = df['ma_5'] * 0.5 + df['ma_10'] * 0.3 + df['ma_20'] * 0.2
df['avg'] = (df['ma_5'] + df['ma_10'] + df['ma_20']) / 3
df['mid'] = df.apply(mid, axis = 1)
df


Out[83]:
ma_20 ma_10 ma_5 comp wtd_avg avg mid
77 27.74 27.74 27.74 29.86 27.740 27.740000 27.74
76 28.80 28.80 28.80 32.08 28.800 28.800000 28.80
75 29.89 29.89 29.89 36.12 29.890 29.890000 29.89
74 31.45 31.45 31.45 38.88 31.450 31.450000 31.45
73 32.94 32.94 32.94 31.82 32.940 32.940000 32.94
72 32.75 32.75 33.75 60.80 33.250 33.083333 32.75
71 36.76 36.76 39.94 38.67 38.350 37.820000 36.76
70 37.00 37.00 41.26 53.16 39.130 38.420000 37.00
69 38.79 38.79 44.67 41.16 41.730 40.750000 38.79
68 39.03 39.03 45.12 42.52 42.075 41.060000 39.03
67 39.35 40.51 47.26 28.70 43.653 42.373333 40.51
66 38.46 40.39 40.84 19.85 40.229 39.896667 40.39
65 37.03 39.17 37.08 43.70 37.697 37.760000 37.08
64 37.50 39.93 35.18 61.94 37.069 37.536667 37.50
63 39.13 42.23 39.34 34.80 40.165 40.233333 39.34
62 38.86 42.53 37.80 44.85 39.431 39.730000 38.86
61 39.22 40.94 41.03 24.63 40.641 40.396667 40.94
60 38.41 39.53 41.99 31.55 40.536 39.976667 39.53
59 38.04 37.37 39.56 41.28 38.599 38.323333 38.04
58 38.21 37.38 35.42 43.08 36.566 37.003333 37.38
57 38.97 37.44 37.08 38.84 37.566 37.830000 37.44
56 39.42 38.45 35.87 28.80 37.354 37.913333 38.45
55 39.26 39.35 36.71 19.51 38.012 38.440000 39.26
54 38.43 36.93 34.30 41.62 35.915 36.553333 36.93
53 38.56 34.90 34.37 41.08 35.367 35.943333 34.90
52 39.03 35.52 33.97 18.53 35.447 36.173333 35.52
51 36.91 32.89 29.91 36.00 32.204 33.236667 32.89
50 36.78 34.03 31.35 41.16 33.240 34.053333 34.03
49 36.18 34.99 35.68 28.15 35.573 35.616667 35.68
48 35.53 33.68 32.98 35.38 33.700 34.063333 33.68
... ... ... ... ... ... ... ...
29 39.04 45.64 51.44 46.03 47.220 45.373333 45.64
28 39.93 46.04 54.92 24.69 49.258 46.963333 46.04
27 39.40 44.42 52.84 22.32 47.626 45.553333 44.42
26 39.53 42.80 42.71 48.28 42.101 41.680000 42.71
25 40.06 44.15 36.79 12.40 39.652 40.333333 40.06
24 39.37 41.09 30.74 28.68 35.571 37.066667 39.37
23 39.60 41.10 27.27 59.29 33.885 35.990000 39.60
22 40.62 43.52 34.19 35.38 38.275 39.443333 40.62
21 40.36 39.76 36.81 18.72 38.405 38.976667 39.76
20 39.43 33.84 30.89 31.45 33.483 34.720000 33.84
19 39.18 32.72 34.70 29.83 35.002 35.533333 34.70
18 38.57 31.10 34.93 40.96 34.509 34.866667 34.93
17 38.58 32.73 31.27 33.74 33.170 34.193333 32.73
16 38.34 33.87 30.94 31.27 33.299 34.383333 33.87
15 38.16 32.17 33.45 25.82 34.008 34.593333 33.45
14 37.30 33.51 32.32 32.11 33.673 34.376667 33.51
13 37.48 33.86 32.78 51.94 34.044 34.706667 33.86
12 38.32 33.12 34.97 48.24 35.085 35.470000 34.97
11 37.08 34.41 37.88 33.00 36.679 36.456667 37.08
10 34.84 35.84 38.22 42.72 36.830 36.300000 35.84
9 34.84 36.96 41.60 61.42 38.856 37.800000 36.96
8 35.61 40.12 47.46 36.90 42.888 41.063333 40.12
7 36.22 39.72 44.46 46.91 41.390 40.133333 39.72
6 37.45 41.03 44.19 44.43 41.894 40.890000 41.03
5 37.26 42.35 46.48 35.49 43.397 42.030000 42.35
4 38.41 43.32 45.03 59.20 43.193 42.253333 43.32
3 39.94 46.03 44.59 41.40 44.092 43.520000 44.59
2 39.05 44.97 45.49 67.78 44.046 43.170000 44.97
1 40.67 46.93 49.66 45.24 47.043 45.753333 46.93
0 41.99 48.15 49.82 35.23 47.753 46.653333 48.15

78 rows × 7 columns


In [84]:
df['comp'].cov(df['wtd_avg'])


Out[84]:
7.0017045754245837

In [85]:
df['comp'].cov(df['avg'])


Out[85]:
5.1354504495504507

In [86]:
df['comp'].cov(df['mid'])


Out[86]:
7.0575483516483564

In [87]:
df[['ma_20', 'ma_10', 'ma_5', 'comp']].plot(title = 'Testing', figsize = (17, 12), grid = True)


Out[87]:
<matplotlib.axes._subplots.AxesSubplot at 0x10b5f04e0>

In [8]:
all_players = nba_py.player.PlayerList(season = '2017-18').info()

In [12]:
home_team_player = all_players[all_players['TEAM_ID'].isin(games['HOME_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
home_team_player['Location'] = 'HOME'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-cb88d241d1d0> in <module>()
      1 home_team_player = all_players[all_players['TEAM_ID'].isin(games['HOME_TEAM_ID'])][['PERSON_ID', 'TEAM_ID']]
      2 home_team_player['Location'] = 'HOME'
----> 3 home_team_player['Game_ID'] = home_team_player['TEAM_ID'].map(games[['HOME_TEAM_ID', 'GAME_ID']])
      4 home_team_player

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py in map(self, arg, na_action)
   2156         else:
   2157             # arg is a function
-> 2158             new_values = map_f(values, arg)
   2159 
   2160         return self._constructor(new_values,

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer (pandas/_libs/lib.c:66440)()

TypeError: 'DataFrame' object is not callable

In [18]:
game_team = pd.concat([games[['HOME_TEAM_ID', 'GAME_ID']].rename(columns = {'HOME_TEAM_ID': 'TEAM_ID'}), 
                       games[['VISITOR_TEAM_ID', 'GAME_ID']].rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID'})])
game_team


Out[18]:
TEAM_ID GAME_ID
0 1610612737 0021600508
1 1610612748 0021600509
2 1610612754 0021600510
3 1610612750 0021600511
4 1610612747 0021600512
0 1610612759 0021600508
1 1610612765 0021600509
2 1610612753 0021600510
3 1610612757 0021600511
4 1610612761 0021600512

In [29]:
team_team = pd.concat([games[['HOME_TEAM_ID', 'VISITOR_TEAM_ID']].rename(columns = {'HOME_TEAM_ID': 'TEAM_ID', 'VISITOR_TEAM_ID': 'Against_Team_ID'}), 
                       games[['VISITOR_TEAM_ID', 'HOME_TEAM_ID']].rename(columns = {'VISITOR_TEAM_ID': 'TEAM_ID', 'HOME_TEAM_ID': 'Against_Team_ID'})])

team_team


Out[29]:
TEAM_ID Against_Team_ID
0 1610612737 1610612759
1 1610612748 1610612765
2 1610612754 1610612753
3 1610612750 1610612757
4 1610612747 1610612761
0 1610612759 1610612737
1 1610612765 1610612748
2 1610612753 1610612754
3 1610612757 1610612750
4 1610612761 1610612747

In [3]:
df_1 = pd.DataFrame(np.random.randn(12).reshape(3, 4))
df_1


Out[3]:
0 1 2 3
0 1.336856 0.713260 0.395341 0.664638
1 -0.986961 -1.159164 0.457794 0.014580
2 0.163043 1.789720 0.079904 0.416343

In [4]:
df_1['4'], df_1['5'] = ['four', 'five']
df_1


Out[4]:
0 1 2 3 4 5
0 1.336856 0.713260 0.395341 0.664638 four five
1 -0.986961 -1.159164 0.457794 0.014580 four five
2 0.163043 1.789720 0.079904 0.416343 four five

In [14]:
nba_py.player.PlayerLastNGamesSplits('200768').last10()


Out[14]:
GROUP_SET GROUP_VALUE GP W L W_PCT MIN FGM FGA FG_PCT ... BLK_RANK BLKA_RANK PF_RANK PFD_RANK PTS_RANK PLUS_MINUS_RANK DD2_RANK TD3_RANK CFID CFPARAMS
0 Last 10 Games 2016-17 10 7 3 0.7 36.2 6.1 14.1 0.433 ... 1 1 1 1 1 1 1 1 36 2016-17

1 rows × 60 columns


In [5]:
nba_py.team.TeamGameLogs('1610612759', season_type = 'Playoffs').info()


Out[5]:
Team_ID Game_ID GAME_DATE MATCHUP WL W L W_PCT MIN FGM ... FT_PCT OREB DREB REB AST STL BLK TOV PF PTS
0 1610612759 0041600314 MAY 22, 2017 SAS vs. GSW L 0 4 0.000 240 41 ... 0.781 12 30 42 33 14 4 8 20 115
1 1610612759 0041600313 MAY 20, 2017 SAS vs. GSW L 0 3 0.000 240 44 ... 0.600 15 31 46 22 10 3 15 21 108
2 1610612759 0041600312 MAY 16, 2017 SAS @ GSW L 0 2 0.000 240 37 ... 0.857 22 27 49 21 11 5 18 17 100
3 1610612759 0041600311 MAY 14, 2017 SAS @ GSW L 0 1 0.000 240 40 ... 0.857 14 23 37 19 13 3 17 24 111
4 1610612759 0041600236 MAY 11, 2017 SAS @ HOU W 4 2 0.667 240 51 ... 0.538 14 46 60 32 8 9 7 21 114
5 1610612759 0041600235 MAY 09, 2017 SAS vs. HOU W 3 2 0.600 265 42 ... 0.652 18 34 52 24 7 5 7 18 110
6 1610612759 0041600234 MAY 07, 2017 SAS @ HOU L 2 2 0.500 240 44 ... 0.500 11 32 43 23 8 6 14 13 104
7 1610612759 0041600233 MAY 05, 2017 SAS @ HOU W 2 1 0.667 240 39 ... 0.800 12 37 49 23 7 8 20 17 103
8 1610612759 0041600232 MAY 03, 2017 SAS vs. HOU W 1 1 0.500 240 48 ... 0.889 16 32 48 27 5 7 7 16 121
9 1610612759 0041600231 MAY 01, 2017 SAS vs. HOU L 0 1 0.000 240 31 ... 0.778 11 34 45 19 10 2 15 18 99
10 1610612759 0041600156 APR 27, 2017 SAS @ MEM W 4 2 0.667 240 37 ... 0.880 16 30 46 13 4 0 12 20 103
11 1610612759 0041600155 APR 25, 2017 SAS vs. MEM W 3 2 0.600 240 42 ... 0.818 10 24 34 23 7 2 7 17 116
12 1610612759 0041600154 APR 22, 2017 SAS @ MEM L 2 2 0.500 265 41 ... 1.000 8 29 37 16 11 3 9 24 108
13 1610612759 0041600153 APR 20, 2017 SAS @ MEM L 2 1 0.667 240 34 ... 0.571 12 30 42 16 1 3 12 21 94
14 1610612759 0041600152 APR 17, 2017 SAS vs. MEM W 2 0 1.000 240 28 ... 0.969 5 34 39 14 4 4 14 13 96
15 1610612759 0041600151 APR 15, 2017 SAS vs. MEM W 1 0 1.000 240 41 ... 0.760 8 34 42 16 4 11 9 20 111

16 rows × 27 columns


In [17]:
nba_py.game.Boxscore('0041600314').player_stats()


Out[17]:
GAME_ID TEAM_ID TEAM_ABBREVIATION TEAM_CITY PLAYER_ID PLAYER_NAME START_POSITION COMMENT MIN FGM ... OREB DREB REB AST STL BLK TO PF PTS PLUS_MINUS
0 0041600314 1610612744 GSW Golden State 201142 Kevin Durant F 37:37 10.0 ... 1.0 11.0 12.0 4.0 0.0 1.0 5.0 3.0 29.0 19.0
1 0041600314 1610612744 GSW Golden State 1627775 Patrick McCaw F 16:60 2.0 ... 1.0 3.0 4.0 2.0 0.0 1.0 0.0 3.0 6.0 12.0
2 0041600314 1610612744 GSW Golden State 203110 Draymond Green C 34:05 5.0 ... 0.0 8.0 8.0 8.0 2.0 0.0 2.0 3.0 16.0 20.0
3 0041600314 1610612744 GSW Golden State 202691 Klay Thompson G 33:55 3.0 ... 0.0 6.0 6.0 1.0 0.0 0.0 0.0 2.0 10.0 7.0
4 0041600314 1610612744 GSW Golden State 201939 Stephen Curry G 33:54 14.0 ... 0.0 5.0 5.0 6.0 0.0 0.0 6.0 2.0 36.0 15.0
5 0041600314 1610612744 GSW Golden State 201580 JaVale McGee 6:56 1.0 ... 1.0 2.0 3.0 0.0 0.0 1.0 1.0 1.0 2.0 -4.0
6 0041600314 1610612744 GSW Golden State 2738 Andre Iguodala 22:26 3.0 ... 1.0 5.0 6.0 3.0 2.0 0.0 2.0 3.0 7.0 4.0
7 0041600314 1610612744 GSW Golden State 2733 Shaun Livingston 14:54 2.0 ... 0.0 1.0 1.0 2.0 0.0 0.0 0.0 5.0 5.0 7.0
8 0041600314 1610612744 GSW Golden State 2561 David West 13:43 2.0 ... 0.0 3.0 3.0 1.0 0.0 0.0 0.0 2.0 4.0 0.0
9 0041600314 1610612744 GSW Golden State 203546 Ian Clark 17:52 5.0 ... 1.0 1.0 2.0 2.0 2.0 0.0 1.0 0.0 12.0 -1.0
10 0041600314 1610612744 GSW Golden State 2440 Matt Barnes 3:20 0.0 ... 0.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 -5.0
11 0041600314 1610612744 GSW Golden State 203949 James Michael McAdoo 2:25 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -3.0
12 0041600314 1610612744 GSW Golden State 1627745 Damian Jones 1:54 1.0 ... 0.0 2.0 2.0 0.0 1.0 0.0 0.0 0.0 2.0 -1.0
13 0041600314 1610612759 SAS San Antonio 203613 Jonathon Simmons F 24:31 5.0 ... 1.0 4.0 5.0 4.0 1.0 1.0 0.0 3.0 13.0 -8.0
14 0041600314 1610612759 SAS San Antonio 1938 Manu Ginobili F 32:10 6.0 ... 1.0 0.0 1.0 7.0 3.0 0.0 1.0 4.0 15.0 -21.0
15 0041600314 1610612759 SAS San Antonio 200746 LaMarcus Aldridge C 22:02 4.0 ... 2.0 5.0 7.0 3.0 0.0 0.0 0.0 2.0 8.0 -18.0
16 0041600314 1610612759 SAS San Antonio 201980 Danny Green G 22:29 3.0 ... 1.0 3.0 4.0 3.0 1.0 1.0 0.0 1.0 9.0 -16.0
17 0041600314 1610612759 SAS San Antonio 201988 Patty Mills G 30:03 4.0 ... 1.0 3.0 4.0 3.0 0.0 1.0 3.0 1.0 14.0 -24.0
18 0041600314 1610612759 SAS San Antonio 203937 Kyle Anderson 26:59 8.0 ... 2.0 5.0 7.0 2.0 4.0 0.0 0.0 1.0 20.0 -1.0
19 0041600314 1610612759 SAS San Antonio 1627749 Dejounte Murray 29:03 2.0 ... 1.0 1.0 2.0 7.0 5.0 0.0 3.0 3.0 9.0 -1.0
20 0041600314 1610612759 SAS San Antonio 1627854 Bryn Forbes 23:51 2.0 ... 0.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 8.0 13.0
21 0041600314 1610612759 SAS San Antonio 2200 Pau Gasol 23:04 6.0 ... 3.0 6.0 9.0 3.0 0.0 0.0 0.0 3.0 14.0 2.0
22 0041600314 1610612759 SAS San Antonio 203473 Dewayne Dedmon 2:54 0.0 ... 0.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 3.0 2.0
23 0041600314 1610612759 SAS San Antonio 202722 Davis Bertans 2:54 1.0 ... 0.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 2.0 2.0
24 0041600314 1610612759 SAS San Antonio 201202 Joel Anthony DNP - Coach's Decision None NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
25 0041600314 1610612759 SAS San Antonio 202695 Kawhi Leonard DND - Sprained Left ankle None NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

26 rows × 28 columns


In [15]:
nba_py.player.PlayerGeneralSplits(2738).location()


Out[15]:
GROUP_SET GROUP_VALUE GP W L W_PCT MIN FGM FGA FG_PCT ... BLK_RANK BLKA_RANK PF_RANK PFD_RANK PTS_RANK PLUS_MINUS_RANK DD2_RANK TD3_RANK CFID CFPARAMS
0 Location Home 40 35 5 0.875 25.4 2.8 5.1 0.554 ... 1 2 1 1 2 1 1 1 28 Home
1 Location Road 36 29 7 0.806 27.3 2.9 5.9 0.502 ... 2 1 2 2 1 2 1 1 28 Road

2 rows × 60 columns


In [ ]: