Spaces:
Running
Running
James McCool
commited on
Commit
·
82e74ae
1
Parent(s):
4429236
Refactor scoring percentage calculations to accommodate multiple slate types for Draftkings and Fanduel. Update game format keys for clarity and enhance player display by including team information. Adjust data handling for ownership metrics and streamline the display of scoring statistics based on user-selected views.
Browse files
app.py
CHANGED
@@ -16,8 +16,8 @@ def init_conn():
|
|
16 |
|
17 |
db = init_conn()
|
18 |
|
19 |
-
game_format = {'Win
|
20 |
-
'Fifth Inning Lead Percentage': '{:.2%}', '8+
|
21 |
|
22 |
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
|
23 |
'4x%': '{:.2%}'}
|
@@ -53,10 +53,17 @@ def init_baselines():
|
|
53 |
cursor = collection.find()
|
54 |
team_frame = pd.DataFrame(cursor)
|
55 |
scoring_percentages = team_frame.drop(columns=['_id'])
|
56 |
-
scoring_percentages = scoring_percentages[['Names', 'Avg First Inning', 'First Inning Lead Percentage', 'Avg Fifth Inning', 'Fifth Inning Lead Percentage', 'Avg Score', '8+ runs', 'Win Percentage',
|
|
|
|
|
57 |
scoring_percentages['8+ runs'] = scoring_percentages['8+ runs'].replace('%', '', regex=True).astype(float)
|
58 |
scoring_percentages['Win Percentage'] = scoring_percentages['Win Percentage'].replace('%', '', regex=True).astype(float)
|
59 |
-
scoring_percentages['Top Score'] = scoring_percentages['Top Score'].replace('', np.nan).astype(float)
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
return roo_data, sd_roo_data, scoring_percentages, dk_roo, fd_roo, dk_id_map, fd_id_map
|
62 |
|
@@ -238,20 +245,40 @@ with tab1:
|
|
238 |
st.header("Scoring Percentages")
|
239 |
with st.expander("Info and Filters"):
|
240 |
with st.container():
|
241 |
-
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', '
|
242 |
own_var1 = st.radio("How would you like to display team ownership?", ('Sum', 'Average'), key='own_var1')
|
243 |
|
244 |
-
if
|
245 |
-
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
dk_hitters_only = dk_roo[dk_roo['pos_group'] != 'Pitchers']
|
250 |
-
|
|
|
|
|
|
|
|
|
|
|
251 |
dk_hitters_only = dk_hitters_only.replace('CWS', 'CHW')
|
252 |
dk_team_ownership = dk_hitters_only.groupby('Team')['Own%'].sum().reset_index()
|
253 |
fd_hitters_only = fd_roo[fd_roo['pos_group'] != 'Pitchers']
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
255 |
fd_hitters_only = fd_hitters_only.replace('CWS', 'CHW')
|
256 |
fd_team_ownership = fd_hitters_only.groupby('Team')['Own%'].sum().reset_index()
|
257 |
scoring_percentages = scoring_percentages.merge(dk_team_ownership, left_on='Names', right_on='Team', how='left')
|
@@ -260,14 +287,40 @@ with tab1:
|
|
260 |
scoring_percentages = scoring_percentages.merge(fd_team_ownership, left_on='Names', right_on='Team', how='left')
|
261 |
scoring_percentages.rename(columns={'Own%': 'FD Own%'}, inplace=True)
|
262 |
scoring_percentages.drop('Team', axis=1, inplace=True)
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
scoring_percentages = scoring_percentages.sort_values(by='8+ runs', ascending=False)
|
267 |
-
|
|
|
|
|
|
|
268 |
|
269 |
if view_var == "Simple":
|
270 |
-
scoring_percentages = scoring_percentages[['Names', '
|
271 |
st.dataframe(scoring_percentages.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
272 |
elif view_var == "Advanced":
|
273 |
st.dataframe(scoring_percentages.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
@@ -343,7 +396,7 @@ with tab2:
|
|
343 |
|
344 |
if view_var == "Simple":
|
345 |
try:
|
346 |
-
player_roo_disp = player_roo_disp[['Player', 'Position', 'Salary', 'Median', 'Ceiling', 'Own%']]
|
347 |
st.dataframe(player_roo_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
348 |
except:
|
349 |
st.dataframe(player_roo_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
|
|
16 |
|
17 |
db = init_conn()
|
18 |
|
19 |
+
game_format = {'Win%': '{:.2%}','First Inning Lead Percentage': '{:.2%}', 'Top Score': '{:.2%}',
|
20 |
+
'Fifth Inning Lead Percentage': '{:.2%}', '8+ Runs': '{:.2%}', 'LevX': '{:.2%}'}
|
21 |
|
22 |
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
|
23 |
'4x%': '{:.2%}'}
|
|
|
53 |
cursor = collection.find()
|
54 |
team_frame = pd.DataFrame(cursor)
|
55 |
scoring_percentages = team_frame.drop(columns=['_id'])
|
56 |
+
scoring_percentages = scoring_percentages[['Names', 'Avg First Inning', 'First Inning Lead Percentage', 'Avg Fifth Inning', 'Fifth Inning Lead Percentage', 'Avg Score', '8+ runs', 'Win Percentage',
|
57 |
+
'DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'DK Main Top Score', 'FD Main Top Score', 'DK Secondary Top Score', 'FD Secondary Top Score',
|
58 |
+
'DK Turbo Top Score', 'FD Turbo Top Score']]
|
59 |
scoring_percentages['8+ runs'] = scoring_percentages['8+ runs'].replace('%', '', regex=True).astype(float)
|
60 |
scoring_percentages['Win Percentage'] = scoring_percentages['Win Percentage'].replace('%', '', regex=True).astype(float)
|
61 |
+
scoring_percentages['DK Main Top Score'] = scoring_percentages['DK Main Top Score'].replace('', np.nan).astype(float)
|
62 |
+
scoring_percentages['FD Main Top Score'] = scoring_percentages['FD Main Top Score'].replace('', np.nan).astype(float)
|
63 |
+
scoring_percentages['DK Secondary Top Score'] = scoring_percentages['DK Secondary Top Score'].replace('', np.nan).astype(float)
|
64 |
+
scoring_percentages['FD Secondary Top Score'] = scoring_percentages['FD Secondary Top Score'].replace('', np.nan).astype(float)
|
65 |
+
scoring_percentages['DK Turbo Top Score'] = scoring_percentages['DK Turbo Top Score'].replace('', np.nan).astype(float)
|
66 |
+
scoring_percentages['FD Turbo Top Score'] = scoring_percentages['FD Turbo Top Score'].replace('', np.nan).astype(float)
|
67 |
|
68 |
return roo_data, sd_roo_data, scoring_percentages, dk_roo, fd_roo, dk_id_map, fd_id_map
|
69 |
|
|
|
245 |
st.header("Scoring Percentages")
|
246 |
with st.expander("Info and Filters"):
|
247 |
with st.container():
|
248 |
+
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Secondary Slate', 'Turbo Slate'), key='slate_var1')
|
249 |
own_var1 = st.radio("How would you like to display team ownership?", ('Sum', 'Average'), key='own_var1')
|
250 |
|
251 |
+
if site_var == 'Draftkings':
|
252 |
+
if slate_var1 == 'Main Slate':
|
253 |
+
scoring_percentages = scoring_percentages[scoring_percentages['DK Main Slate'] == 1]
|
254 |
+
elif slate_var1 == 'Secondary Slate':
|
255 |
+
scoring_percentages = scoring_percentages[scoring_percentages['DK Secondary Slate'] == 1]
|
256 |
+
elif slate_var1 == 'Turbo Slate':
|
257 |
+
scoring_percentages = scoring_percentages[scoring_percentages['DK Turbo Slate'] == 1]
|
258 |
+
elif site_var == 'Fanduel':
|
259 |
+
if slate_var1 == 'Main Slate':
|
260 |
+
scoring_percentages = scoring_percentages[scoring_percentages['FD Main Slate'] == 1]
|
261 |
+
elif slate_var1 == 'Secondary Slate':
|
262 |
+
scoring_percentages = scoring_percentages[scoring_percentages['FD Secondary Slate'] == 1]
|
263 |
+
elif slate_var1 == 'Turbo Slate':
|
264 |
+
scoring_percentages = scoring_percentages[scoring_percentages['FD Turbo Slate'] == 1]
|
265 |
|
266 |
dk_hitters_only = dk_roo[dk_roo['pos_group'] != 'Pitchers']
|
267 |
+
if slate_var1 == 'Main Slate':
|
268 |
+
dk_hitters_only = dk_hitters_only[dk_hitters_only['Slate'] == 'main_slate']
|
269 |
+
elif slate_var1 == 'Secondary Slate':
|
270 |
+
dk_hitters_only = dk_hitters_only[dk_hitters_only['Slate'] == 'secondary_slate']
|
271 |
+
elif slate_var1 == 'Turbo Slate':
|
272 |
+
dk_hitters_only = dk_hitters_only[dk_hitters_only['Slate'] == 'turbo_slate']
|
273 |
dk_hitters_only = dk_hitters_only.replace('CWS', 'CHW')
|
274 |
dk_team_ownership = dk_hitters_only.groupby('Team')['Own%'].sum().reset_index()
|
275 |
fd_hitters_only = fd_roo[fd_roo['pos_group'] != 'Pitchers']
|
276 |
+
if slate_var1 == 'Main Slate':
|
277 |
+
fd_hitters_only = fd_hitters_only[fd_hitters_only['Slate'] == 'main_slate']
|
278 |
+
elif slate_var1 == 'Secondary Slate':
|
279 |
+
fd_hitters_only = fd_hitters_only[fd_hitters_only['Slate'] == 'secondary_slate']
|
280 |
+
elif slate_var1 == 'Turbo Slate':
|
281 |
+
fd_hitters_only = fd_hitters_only[fd_hitters_only['Slate'] == 'turbo_slate']
|
282 |
fd_hitters_only = fd_hitters_only.replace('CWS', 'CHW')
|
283 |
fd_team_ownership = fd_hitters_only.groupby('Team')['Own%'].sum().reset_index()
|
284 |
scoring_percentages = scoring_percentages.merge(dk_team_ownership, left_on='Names', right_on='Team', how='left')
|
|
|
287 |
scoring_percentages = scoring_percentages.merge(fd_team_ownership, left_on='Names', right_on='Team', how='left')
|
288 |
scoring_percentages.rename(columns={'Own%': 'FD Own%'}, inplace=True)
|
289 |
scoring_percentages.drop('Team', axis=1, inplace=True)
|
290 |
+
if site_var == 'Draftkings':
|
291 |
+
if slate_var1 == 'Main Slate':
|
292 |
+
scoring_percentages['DK LevX'] = scoring_percentages['DK Main Top Score'].rank(pct=True).astype(float) - scoring_percentages['DK Own%'].rank(pct=True).astype(float)
|
293 |
+
scoring_percentages = scoring_percentages.rename(columns={'DK Main Top Score': 'Top Score'})
|
294 |
+
scoring_percentages = scoring_percentages.drop(['DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'FD Main Top Score', 'DK Secondary Top Score', 'FD Secondary Top Score', 'DK Turbo Top Score', 'FD Turbo Top Score'], axis=1)
|
295 |
+
elif slate_var1 == 'Secondary Slate':
|
296 |
+
scoring_percentages['DK LevX'] = scoring_percentages['DK Secondary Top Score'].rank(pct=True).astype(float) - scoring_percentages['DK Own%'].rank(pct=True).astype(float)
|
297 |
+
scoring_percentages = scoring_percentages.rename(columns={'DK Secondary Top Score': 'Top Score'})
|
298 |
+
scoring_percentages = scoring_percentages.drop(['DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'FD Main Top Score', 'DK Main Top Score', 'FD Secondary Top Score', 'DK Turbo Top Score', 'FD Turbo Top Score'], axis=1)
|
299 |
+
elif slate_var1 == 'Turbo Slate':
|
300 |
+
scoring_percentages['DK LevX'] = scoring_percentages['DK Turbo Top Score'].rank(pct=True).astype(float) - scoring_percentages['DK Own%'].rank(pct=True).astype(float)
|
301 |
+
scoring_percentages = scoring_percentages.rename(columns={'DK Turbo Top Score': 'Top Score'})
|
302 |
+
scoring_percentages = scoring_percentages.drop(['DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'FD Main Top Score', 'DK Main Top Score', 'FD Secondary Top Score', 'DK Secondary Top Score', 'FD Turbo Top Score'], axis=1)
|
303 |
+
elif site_var == 'Fanduel':
|
304 |
+
if slate_var1 == 'Main Slate':
|
305 |
+
scoring_percentages['FD LevX'] = scoring_percentages['FD Main Top Score'].rank(pct=True).astype(float) - scoring_percentages['FD Own%'].rank(pct=True).astype(float)
|
306 |
+
scoring_percentages = scoring_percentages.rename(columns={'FD Main Top Score': 'Top Score'})
|
307 |
+
scoring_percentages = scoring_percentages.drop(['DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'DK Main Top Score', 'DK Secondary Top Score', 'FD Secondary Top Score', 'DK Turbo Top Score', 'FD Turbo Top Score'], axis=1)
|
308 |
+
elif slate_var1 == 'Secondary Slate':
|
309 |
+
scoring_percentages['FD LevX'] = scoring_percentages['FD Secondary Top Score'].rank(pct=True).astype(float) - scoring_percentages['FD Own%'].rank(pct=True).astype(float)
|
310 |
+
scoring_percentages = scoring_percentages.rename(columns={'FD Secondary Top Score': 'Top Score'})
|
311 |
+
scoring_percentages = scoring_percentages.drop(['DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'FD Main Top Score', 'DK Main Top Score', 'DK Secondary Top Score', 'DK Turbo Top Score', 'FD Turbo Top Score'], axis=1)
|
312 |
+
elif slate_var1 == 'Turbo Slate':
|
313 |
+
scoring_percentages['FD LevX'] = scoring_percentages['FD Turbo Top Score'].rank(pct=True).astype(float) - scoring_percentages['FD Own%'].rank(pct=True).astype(float)
|
314 |
+
scoring_percentages = scoring_percentages.rename(columns={'FD Turbo Top Score': 'Top Score'})
|
315 |
+
scoring_percentages = scoring_percentages.drop(['DK Main Slate', 'DK Secondary Slate', 'DK Turbo Slate', 'FD Main Slate', 'FD Secondary Slate', 'FD Turbo Slate', 'FD Main Top Score', 'DK Main Top Score', 'FD Secondary Top Score', 'DK Secondary Top Score', 'DK Turbo Top Score'], axis=1)
|
316 |
scoring_percentages = scoring_percentages.sort_values(by='8+ runs', ascending=False)
|
317 |
+
if site_var == 'Draftkings':
|
318 |
+
scoring_percentages = scoring_percentages.rename(columns={'DK LevX': 'LevX', 'DK Own%': 'Own%', 'Avg Score': 'Runs', 'Win Percentage': 'Win%', '8+ runs': '8+ Runs'})
|
319 |
+
elif site_var == 'Fanduel':
|
320 |
+
scoring_percentages = scoring_percentages.rename(columns={'FD LevX': 'LevX', 'FD Own%': 'Own%', 'Avg Score': 'Runs', 'Win Percentage': 'Win%', '8+ runs': '8+ Runs'})
|
321 |
|
322 |
if view_var == "Simple":
|
323 |
+
scoring_percentages = scoring_percentages[['Names', 'Runs', '8+ Runs', 'Win%', 'LevX', 'Own%']]
|
324 |
st.dataframe(scoring_percentages.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
325 |
elif view_var == "Advanced":
|
326 |
st.dataframe(scoring_percentages.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
|
|
396 |
|
397 |
if view_var == "Simple":
|
398 |
try:
|
399 |
+
player_roo_disp = player_roo_disp[['Player', 'Position', 'Team', 'Salary', 'Median', 'Ceiling', 'Own%']]
|
400 |
st.dataframe(player_roo_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), height=750, use_container_width = True, hide_index=True)
|
401 |
except:
|
402 |
st.dataframe(player_roo_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), height=750, use_container_width = True, hide_index=True)
|