nesticot commited on
Commit
d1f3cb7
·
1 Parent(s): ddb5a97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -48
app.py CHANGED
@@ -170,6 +170,18 @@ team_dict = team_abv.set_index('team_abv').to_dict()['team_name']
170
 
171
  yahoo_nhl_df = pd.read_csv('yahoo_to_nhl.csv', encoding='unicode_escape')
172
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  def nat_stat_convert(df):
174
  for i in range(0,len(df.columns)):
175
  if df.columns[i][-3:]=='/60':
@@ -263,70 +275,69 @@ def server(input, output, session):
263
  if type(n_1) is not int:
264
  n_1 = 1
265
 
266
- if n_2 == 0:
267
  list_of_columns.remove(f'L{str(n_2)} PP TOI')
268
  list_of_columns.remove(f'L{str(n_2)} PP%')
269
  list_of_columns_name.remove(f'L{str(n_2)} PP TOI')
270
  list_of_columns_name.remove(f'L{str(n_2)} PP%')
271
 
272
- if n_3 == 0:
273
  list_of_columns.remove(f'L{str(n_3)} PP TOI')
274
  list_of_columns.remove(f'L{str(n_3)} PP%')
275
  list_of_columns_name.remove(f'L{str(n_3)} PP TOI')
276
  list_of_columns_name.remove(f'L{str(n_3)} PP%')
277
 
 
 
278
  start_date ='2023-09-01'
279
  end_date = '2024-05-01'
280
 
281
- df_pp_1 = nat_stat_trick_range_pp_gp(rookie='n',start_date = start_date,end_date = end_date, sit='pp',gp=n_1)
282
- df_pp_2 = nat_stat_trick_range_pp_gp(rookie='n',start_date = start_date,end_date = end_date, sit='pp',gp=n_2)
283
- df_pp_3 = nat_stat_trick_range_pp_gp(rookie='n',start_date = start_date,end_date = end_date, sit='pp',gp=n_3)
284
-
285
-
286
- df_all_pp_1 = nat_stat_convert(df_pp_1)
287
- df_all_pp_2 = nat_stat_convert(df_pp_2)
288
- df_all_pp_3 = nat_stat_convert(df_pp_3)
289
-
290
- df_final = df_all_pp_1.merge( df_all_pp_2,how='outer',left_on=['player_id'],right_on=['player_id'],suffixes=("","_2"))
291
- df_final = df_final.merge( df_all_pp_3,how='outer',left_on=['player_id'],right_on=['player_id'],suffixes=("_1","_3"))
292
-
293
- time.sleep(2)
294
- team_report_1 = pd.read_html(f'https://www.naturalstattrick.com/teamtable.php?fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&rate=n&team=all&loc=B&gpf=c&gp='+str(n_1)+'&fd=&td=')[0]
295
- time.sleep(2)
296
- team_report_2 = pd.read_html(f'https://www.naturalstattrick.com/teamtable.php?fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&rate=n&team=all&loc=B&gpf=c&gp='+str(n_2)+'&fd=&td=')[0]
297
- time.sleep(2)
298
- team_report_3 = pd.read_html(f'https://www.naturalstattrick.com/teamtable.php?fromseason=fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&rate=n&team=all&loc=B&gpf=c&gp='+str(n_3)+'&fd=&td=')[0]
299
- team_report_1 = team_report_1.merge(team_abv,left_on=['Team'],right_on=['team_name'],how='left')
300
- team_report_2 = team_report_2.merge(team_abv,left_on=['Team'],right_on=['team_name'],how='left')
301
- team_report_3 = team_report_3.merge(team_abv,left_on=['Team'],right_on=['team_name'],how='left')
302
-
303
- test = df_final[['player_id','Player_1','Team_1','Position_1','TOI_1','TOI_2','TOI_3',]]
304
- test.columns = ['player_id','Player','Team','Position','TOI_1','TOI_2','TOI_3',]
305
- test = test.merge(team_report_1[['TOI','team_abv']],how='left',left_on=['Team'],right_on=['team_abv'], suffixes=('','_1_team'))
306
- test = test.merge(team_report_2[['TOI','team_abv']],how='left',left_on=['Team'],right_on=['team_abv'], suffixes=('','_2_team'))
307
- test = test.merge(team_report_3[['TOI','team_abv']],how='left',left_on=['Team'],right_on=['team_abv'], suffixes=('','_3_team'))
308
- test = test.fillna('0:00')
309
-
310
- test['TOI'] = [int(x[:-3])+int(x[-2:])*100/60/100 for x in test['TOI'] ]
311
- test['TOI_1'] = [x if x!='0:00' else 0 for x in test['TOI_1']]
312
- test['TOI_2'] = [x if x!='0:00' else 0 for x in test['TOI_2']]
313
- test['TOI_3'] = [x if x!='0:00' else 0 for x in test['TOI_3']]
314
-
315
- test['TOI_2_team'] = [int(x[:-3])+int(x[-2:])*100/60/100 for x in test['TOI_2_team'] ]
316
- test['TOI_3_team'] = [int(x[:-3])+int(x[-2:])*100/60/100 for x in test['TOI_3_team']]
317
-
318
- test['TOI'] = test['TOI'].astype(float)
319
-
320
- test['PP%_1'] = test['TOI_1'].astype(float)/ test['TOI'].astype(float)
321
- test['PP%_2'] = test['TOI_2'].astype(float)/ test['TOI_2_team'].astype(float)
322
- test['PP%_3'] = test['TOI_3'].astype(float)/ test['TOI_3_team'].astype(float)
323
- test = test.fillna(0)
324
  test['TOI_1'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_1'].astype(float)]
325
  test['TOI_2'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_2'].astype(float)]
326
  test['TOI_3'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_3'].astype(float)]
327
- test = test.drop(['team_abv','team_abv_2_team','team_abv_3_team','TOI','TOI_2_team','TOI_3_team'],axis=1)
328
  test.columns = ['player_id','Player','Team','Position','L'+str(n_1)+' PP TOI','L'+str(n_2)+' PP TOI','L'+str(n_3)+' PP TOI','L'+str(n_1)+' PP%','L'+str(n_2)+' PP%','L'+str(n_3)+' PP%']
329
 
 
330
  yahoo_df = yahoo_df_2.merge(yahoo_nhl_df,left_on = 'player_id',right_on='player_id_yahoo',suffixes=['','_y'])
331
  yahoo_df.nhl_id = yahoo_df.nhl_id.astype(float)
332
  test.player_id = test.player_id.astype(float)
@@ -415,7 +426,8 @@ def server(input, output, session):
415
  **{'border': '1px black solid !important'},subset = ((list(top_d_score.index[:]),top_d_score.columns[:]))).set_properties(**{
416
  'color': 'black'},overwrite=False).set_properties(
417
  **{'border': '1px black solid !important'},subset = ((list(top_d_score.index[:]),top_d_score.columns[:]))).format(
418
- {'L'+str(n_1)+' PP%': '{:.0%}',
 
419
  'L'+str(n_2)+' PP%': '{:.0%}',
420
  'L'+str(n_3)+' PP%': '{:.0%}',
421
  'Roster%': '{:.0%}',
@@ -431,4 +443,4 @@ def server(input, output, session):
431
 
432
 
433
  app = App(app_ui, server)
434
- time.sleep(60)
 
170
 
171
  yahoo_nhl_df = pd.read_csv('yahoo_to_nhl.csv', encoding='unicode_escape')
172
 
173
+
174
+ player_games_df = pd.read_csv('player_games_cards.csv',index_col=[0])
175
+ team_games_df = pd.read_csv('team_games.csv',index_col=[0])
176
+
177
+ team_games_df['game_count'] = team_games_df.groupby('team')['team'].cumcount()+1
178
+ team_games_df['max_games'] = team_games_df.groupby('team').game_count.transform('max')
179
+ team_games_df['abv'] = team_games_df.team.map(team_abv.set_index('team_name')['team_abv'].to_dict())
180
+
181
+ #team_abv = pd.read_csv('team_abv.csv')
182
+
183
+
184
+
185
  def nat_stat_convert(df):
186
  for i in range(0,len(df.columns)):
187
  if df.columns[i][-3:]=='/60':
 
275
  if type(n_1) is not int:
276
  n_1 = 1
277
 
278
+ if (n_2 == 0) or (n_2 == n_1) or (n_2 == None):
279
  list_of_columns.remove(f'L{str(n_2)} PP TOI')
280
  list_of_columns.remove(f'L{str(n_2)} PP%')
281
  list_of_columns_name.remove(f'L{str(n_2)} PP TOI')
282
  list_of_columns_name.remove(f'L{str(n_2)} PP%')
283
 
284
+ if (n_3 == 0) or (n_3 == n_1) or (n_3 == n_2) or (n_3 == None):
285
  list_of_columns.remove(f'L{str(n_3)} PP TOI')
286
  list_of_columns.remove(f'L{str(n_3)} PP%')
287
  list_of_columns_name.remove(f'L{str(n_3)} PP TOI')
288
  list_of_columns_name.remove(f'L{str(n_3)} PP%')
289
 
290
+
291
+
292
  start_date ='2023-09-01'
293
  end_date = '2024-05-01'
294
 
295
+ df_pp_1 = player_games_df.groupby('Player').head(n_1)
296
+ df_pp_2 = player_games_df.groupby('Player').head(n_2)
297
+ df_pp_3 = player_games_df.groupby('Player').head(n_3)
298
+
299
+
300
+ team_games_df_1 = team_games_df.groupby('team').head(n_1)
301
+ team_games_df_2 = team_games_df.groupby('team').head(n_2)
302
+ team_games_df_3 = team_games_df.groupby('team').head(n_3)
303
+
304
+ df_all_pp_1 = df_pp_1.copy()
305
+ df_all_pp_2 = df_pp_2.copy()
306
+ df_all_pp_3 = df_pp_3.copy()
307
+
308
+ df_all_pp_1_final = df_all_pp_1.groupby(['player_id','Player','Team','Position']).sum()[['TOI_pp']].reset_index()
309
+ df_all_pp_2_final = df_all_pp_2.groupby(['player_id','Player','Team','Position']).sum()[['TOI_pp']].reset_index()
310
+ df_all_pp_3_final = df_all_pp_3.groupby(['player_id','Player','Team','Position']).sum()[['TOI_pp']].reset_index()
311
+
312
+ team_games_df_1_final = team_games_df_1.groupby(['abv']).sum()[['pp_toi']].reset_index()
313
+ team_games_df_2_final = team_games_df_2.groupby(['abv']).sum()[['pp_toi']].reset_index()
314
+ team_games_df_3_final = team_games_df_3.groupby(['abv']).sum()[['pp_toi']].reset_index()
315
+
316
+ df_final = df_all_pp_1_final.merge( df_all_pp_2_final,how='outer',left_on=['player_id'],right_on=['player_id'],suffixes=("","_2"))
317
+ df_final = df_final.merge( df_all_pp_3_final,how='outer',left_on=['player_id'],right_on=['player_id'],suffixes=("_1","_3"))
318
+
319
+ team_final = team_games_df_1_final.merge( team_games_df_2_final,how='outer',left_on=['abv'],right_on=['abv'],suffixes=("","_2"))
320
+ team_final = team_final.merge( team_games_df_3_final,how='outer',left_on=['abv'],right_on=['abv'],suffixes=("_1","_3"))
321
+
322
+
323
+ df_final = df_final.merge(team_final,left_on='Team_1',right_on='abv')
324
+
325
+ test = df_final[['player_id','Player_1','Team_1','Position_1','TOI_pp_1','TOI_pp_2','TOI_pp_3','pp_toi_1','pp_toi_2','pp_toi_3']]
326
+ test.columns = ['player_id','Player','Team','Position','TOI_1','TOI_2','TOI_3','pp_toi_1','pp_toi_2','pp_toi_3']
327
+ test = test.fillna('0')
328
+
329
+
330
+ test['PP%_1'] = test['TOI_1'].astype(float)/ test['pp_toi_1'].astype(float)
331
+ test['PP%_2'] = test['TOI_2'].astype(float)/ test['pp_toi_2'].astype(float)
332
+ test['PP%_3'] = test['TOI_3'].astype(float)/ test['pp_toi_3'].astype(float)
333
+ # test = test.fillna(0)
 
 
 
 
334
  test['TOI_1'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_1'].astype(float)]
335
  test['TOI_2'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_2'].astype(float)]
336
  test['TOI_3'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_3'].astype(float)]
337
+ test = test.drop(['pp_toi_1','pp_toi_2','pp_toi_3'],axis=1)
338
  test.columns = ['player_id','Player','Team','Position','L'+str(n_1)+' PP TOI','L'+str(n_2)+' PP TOI','L'+str(n_3)+' PP TOI','L'+str(n_1)+' PP%','L'+str(n_2)+' PP%','L'+str(n_3)+' PP%']
339
 
340
+
341
  yahoo_df = yahoo_df_2.merge(yahoo_nhl_df,left_on = 'player_id',right_on='player_id_yahoo',suffixes=['','_y'])
342
  yahoo_df.nhl_id = yahoo_df.nhl_id.astype(float)
343
  test.player_id = test.player_id.astype(float)
 
426
  **{'border': '1px black solid !important'},subset = ((list(top_d_score.index[:]),top_d_score.columns[:]))).set_properties(**{
427
  'color': 'black'},overwrite=False).set_properties(
428
  **{'border': '1px black solid !important'},subset = ((list(top_d_score.index[:]),top_d_score.columns[:]))).format(
429
+ {
430
+ 'L'+str(n_1)+' PP%': '{:.0%}',
431
  'L'+str(n_2)+' PP%': '{:.0%}',
432
  'L'+str(n_3)+' PP%': '{:.0%}',
433
  'Roster%': '{:.0%}',
 
443
 
444
 
445
  app = App(app_ui, server)
446
+ #time.sleep(60)