Spaces:
Running
Running
File size: 23,531 Bytes
ffe7479 1e9e16a ffe7479 1e9e16a ffe7479 994dc2e ffe7479 8d74161 277956f ffe7479 8d74161 ffe7479 8d74161 ffe7479 45ba68f 535860f ffe7479 8d74161 ffe7479 45ba68f ffe7479 8d74161 ffe7479 45ba68f 5651fbf a49189a ffe7479 8d74161 ffe7479 45ba68f ffe7479 8d74161 ffe7479 45ba68f ffe7479 8d74161 ffe7479 45ba68f ffe7479 8d74161 ffe7479 45ba68f ffe7479 8d74161 ffe7479 45ba68f ffe7479 8d74161 ffe7479 45ba68f ffe7479 3b34f93 ffe7479 103c734 ffe7479 35d5bc3 ffe7479 103c734 ffe7479 35d5bc3 ffe7479 103c734 ffe7479 35d5bc3 ffe7479 103c734 ffe7479 35d5bc3 33c2a96 ffe7479 103c734 ffe7479 35d5bc3 33c2a96 ffe7479 103c734 ffe7479 103c734 ffe7479 103c734 ffe7479 103c734 ffe7479 35d5bc3 ffe7479 94f807d ffe7479 94f807d ffe7479 94f807d ffe7479 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
import pulp
import numpy as np
import pandas as pd
import streamlit as st
import gspread
from itertools import combinations
scope = ['https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive"]
credentials = {
"type": "service_account",
"project_id": "model-sheets-connect",
"private_key_id": "0e0bc2fdef04e771172fe5807392b9d6639d945e",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\ncx0KQ23nZK3ZVvADYy8u/RUn/EDI82QKxTd/DizRLIV81JiNQxDJXSzgkbwKYEDm\n48E8zGvupU8+Nk76xNPakrQKy2Y8+VJlq5psBtGchJTuUSHcXU5Mg2JhQsB376PJ\nsCw552K6Pw8fpeMDJDZuxpKSkaJR6k9G5Dhf5q8HDXnC5Rh/PRFuKJ2GGRpX7n+2\nhT/sCax0J8jfdTy/MDGiDfJqfQrOPrMKELtsGHR9Iv6F4vKiDqXpKfqH+02E9ptz\nBk+MNcbZ3m90M8ShfRu28ebebsASfarNMzc3dk7tb3utHOGXKCf4tF8yYKo7x8BZ\noO9X4gSfAgMBAAECggEAU8ByyMpSKlTCF32TJhXnVJi/kS+IhC/Qn5JUDMuk4LXr\naAEWsWO6kV/ZRVXArjmuSzuUVrXumISapM9Ps5Ytbl95CJmGDiLDwRL815nvv6k3\nUyAS8EGKjz74RpoIoH6E7EWCAzxlnUgTn+5oP9Flije97epYk3H+e2f1f5e1Nn1d\nYNe8U+1HqJgILcxA1TAUsARBfoD7+K3z/8DVPHI8IpzAh6kTHqhqC23Rram4XoQ6\nzj/ZdVBjvnKuazETfsD+Vl3jGLQA8cKQVV70xdz3xwLcNeHsbPbpGBpZUoF73c65\nkAXOrjYl0JD5yAk+hmYhXr6H9c6z5AieuZGDrhmlFQKBgQDzV6LRXmjn4854DP/J\nI82oX2GcI4eioDZPRukhiQLzYerMQBmyqZIRC+/LTCAhYQSjNgMa+ZKyvLqv48M0\n/x398op/+n3xTs+8L49SPI48/iV+mnH7k0WI/ycd4OOKh8rrmhl/0EWb9iitwJYe\nMjTV/QxNEpPBEXfR1/mvrN/lVQKBgQDuhomOxUhWVRVH6x03slmyRBn0Oiw4MW+r\nrt1hlNgtVmTc5Mu+4G0USMZwYuOB7F8xG4Foc7rIlwS7Ic83jMJxemtqAelwOLdV\nXRLrLWJfX8+O1z/UE15l2q3SUEnQ4esPHbQnZowHLm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
"client_email": "gspread-connection@model-sheets-connect.iam.gserviceaccount.com",
"client_id": "100369174533302798535",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
}
gc = gspread.service_account_from_dict(credentials)
st.set_page_config(layout="wide")
wrong_acro = ['AZ', 'WSN', 'WSH', 'TB', 'KC', 'SD', 'CWS', 'SF']
right_acro = ['ARI', 'WAS', 'WAS', 'TBR', 'KCR', 'SDP', 'CHW', 'SFG']
SP_format = {'K%': '{:.2%}', 'BB%': '{:.2%}'}
SP_league_format = ['Strikeoutper', 'Walkper','xBA', 'xSLG', 'BABIP', 'xwOBA', 'AVG', 'True_AVG']
BP_league_format = ['Strikeoutper', 'Walkper','xBA', 'xSLG', 'BABIP', 'xwOBA', 'AVG', 'HWS Ratio']
hitter_format = {'K%': '{:.2%}', 'xHR/PA': '{:.2%}', 'Event/PA': '{:.2%}'}
offense_format = {'8+ For': '{:.2%}', '8+ For L5': '{:.2%}', '8+ For L10': '{:.2%}', 'Trending 8+ For': '{:.2%}'}
defense_format = {'8+ Allowed': '{:.2%}', '8+ Allowed L5': '{:.2%}', '8+ Allowed L10': '{:.2%}', 'Trending 8+ Allowed': '{:.2%}'}
R2_format = {'R2_to_Opp_szn': '{:.2%}', 'R2_to_Opp_sample': '{:.2%}', 'R2_to_Opp L5': '{:.2%}', 'R2_to_Opp L10': '{:.2%}', 'R2_to_Opp_Trend': '{:.2%}'}
data_hold = 'https://docs.google.com/spreadsheets/d/1f42Ergav8K1VsOLOK9MUn7DM_MLMvv4GR2Fy7EfnZTc/edit#gid=500994479'
sh = gc.open_by_url(data_hold)
@st.cache_resource(ttl = 300)
def load_time():
worksheet = sh.worksheet('Timestamp')
raw_stamp = worksheet.acell('a1').value
t_stamp = f"Last update was at {raw_stamp}"
return t_stamp
@st.cache_resource(ttl = 299)
def load_table(URL, specific_tab):
worksheet = sh.worksheet(specific_tab)
load_display = pd.DataFrame(worksheet.get_all_records())
return load_display
@st.cache_resource(ttl = 299)
def True_AVG_Splits_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('True_AVG_Split')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.drop(columns=['HWSr (LHH)', 'HWSr (RHH)', 'HWSr (Overall)', 'Weighted HWSr',])
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats.sort_values(by='Weighted True AVG', ascending=True)
return pitcher_stats
@st.cache_resource(ttl = 299)
def HWSr_Splits_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('True_AVG_Split')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.drop(columns=['True AVG (LHH)', 'True AVG (RHH)', 'True AVG (Overall)', 'Weighted True AVG',])
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats.sort_values(by='Weighted HWSr', ascending=True)
return pitcher_stats
@st.cache_resource(ttl = 299)
def SP_Slate_Stats_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Starting_Pitchers')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats.loc[pitcher_stats['Playing'] == 1]
pitcher_stats = pitcher_stats.drop(columns=['Playing'])
pitcher_stats = pitcher_stats.sort_values(by='True AVG', ascending=True)
return pitcher_stats
@st.cache_resource(ttl = 299)
def RHH_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Pitcher_Data (RHH)')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats.loc[pitcher_stats['Playing'] == 1]
pitcher_stats = pitcher_stats.drop(columns=['Playing', 'Avg IP'])
pitcher_stats = pitcher_stats.sort_values(by='True AVG', ascending=True)
return pitcher_stats
@st.cache_resource(ttl = 299)
def LHH_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Pitcher_Data (LHH)')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats.loc[pitcher_stats['Playing'] == 1]
pitcher_stats = pitcher_stats.drop(columns=['Playing', 'Avg IP'])
pitcher_stats = pitcher_stats.sort_values(by='True AVG', ascending=True)
return pitcher_stats
@st.cache_resource(ttl = 299)
def Full_Stats_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Pitcher_xData')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats[['Player', 'PA', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeoutper', 'Strikeouts', 'Walkper', 'Walks', 'xSLG', 'xwOBA', 'BABIP', 'AVG', 'xBA', 'True_AVG', 'xHRs']]
pitcher_stats = pitcher_stats.sort_values(by='PA', ascending=False)
pitcher_stats = pitcher_stats.drop_duplicates(subset='Player')
pitcher_stats = pitcher_stats.set_index('Player')
return pitcher_stats
@st.cache_resource(ttl = 299)
def Full_RHH_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Pitcher_xData_RHH')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats[['Player', 'PA', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeoutper', 'Strikeouts', 'Walkper', 'Walks', 'xSLG', 'xwOBA', 'BABIP', 'AVG', 'xBA', 'True_AVG', 'xHRs']]
pitcher_stats = pitcher_stats.sort_values(by='PA', ascending=False)
pitcher_stats = pitcher_stats.drop_duplicates(subset='Player')
pitcher_stats = pitcher_stats.set_index('Player')
return pitcher_stats
@st.cache_resource(ttl = 299)
def Full_LHH_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Pitcher_xData_LHH')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
pitcher_stats = pitcher_stats[['Player', 'PA', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeoutper', 'Strikeouts', 'Walkper', 'Walks', 'xSLG', 'xwOBA', 'BABIP', 'AVG', 'xBA', 'True_AVG', 'xHRs']]
pitcher_stats = pitcher_stats.sort_values(by='PA', ascending=False)
pitcher_stats = pitcher_stats.drop_duplicates(subset='Player')
pitcher_stats = pitcher_stats.set_index('Player')
return pitcher_stats
@st.cache_resource(ttl = 299)
def Bullpen_Data_load():
sh = gc.open_by_url(data_hold)
worksheet = sh.worksheet('Bullpen_xData')
pitcher_stats = pd.DataFrame(worksheet.get_all_records())
pitcher_stats.apply(pd.to_numeric, errors='ignore')
pitcher_stats = pitcher_stats.dropna()
for checkVar in range(len(wrong_acro)):
pitcher_stats['Names'] = pitcher_stats['Names'].replace(wrong_acro, right_acro)
pitcher_stats = pitcher_stats.sort_values(by='xSLG', ascending=False)
return pitcher_stats
@st.cache_data
def convert_df_to_csv(df):
return df.to_csv().encode('utf-8')
t_stamp = load_time()
raw_baselines = load_table(data_hold, 'Starting_Pitchers')
pitcher_stats = load_table(data_hold, 'Starting_Pitchers')
hitter_stats = load_table(data_hold, 'DK_Slate_hitters')
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats.apply(pd.to_numeric, errors='ignore')
hitter_stats = hitter_stats.dropna(subset=['Order'])
hitter_stats = hitter_stats.dropna(subset=['Opp_SP'])
macro_tables = load_table(data_hold, 'Macro_Trending')
col1, col2 = st.columns([1, 5])
with col1:
st.info(t_stamp)
if st.button("Load/Reset Data", key='reset1'):
st.cache_data.clear()
t_stamp = load_time()
pitcher_stats = load_table(data_hold, 'Starting_Pitchers')
hitter_stats = load_table(data_hold, 'DK_Slate_hitters')
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Order'])
stat_type_var1 = st.radio("Are you looking at pitchers or hitters?", ('Pitchers', 'Hitters'), key='stat_type_var1')
if stat_type_var1 == 'Pitchers':
stat_var1 = st.radio("What sheets would you like to view?", ('True AVG Splits', 'HWSr Splits', 'Current Slate Stats', 'Stats vs. RHH', 'Stats vs. LHH', 'Full League Stats', 'Full League Stats vs. RHH', 'Full League Stats vs. LHH', 'Bullpen Data'), key='stat_var1')
sp_split1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='sp_split1')
if sp_split1 == 'Specific Games':
sp_var1 = st.multiselect('Which teams would you like to include in the Table?', options = hitter_stats['Team'].unique(), key='sp_var1')
elif sp_split1 == 'Full Slate Run':
sp_var1 = hitter_stats.Team.values.tolist()
elif stat_type_var1 == 'Hitters':
site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
if site_var1 == "Draftkings":
hitter_stats = load_table(data_hold, 'DK_Slate_hitters')
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Order'])
elif site_var1 == "Fanduel":
hitter_stats = load_table(data_hold, 'FD_Slate_Hitters')
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Order'])
stat_var1 = st.radio("What sheets would you like to view?", options = ['Current Slate Player Stats', 'Current Slate Team Stats'], key='stat_var1')
split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
pos_split1 = st.radio("Are you viewing all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1')
if pos_split1 == 'Specific Positions':
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['C', '1B', '2B', '3B', 'SS', 'OF'])
elif pos_split1 == 'All Positions':
pos_var1 = 'All'
if split_var1 == 'Specific Games':
team_var1 = st.multiselect('Which teams would you like to include in the Table?', options = hitter_stats['Team'].unique(), key='team_var1')
elif split_var1 == 'Full Slate Run':
team_var1 = hitter_stats.Team.values.tolist()
with col2:
if stat_type_var1 == 'Pitchers':
if stat_var1 == 'True AVG Splits':
pitcher_stats = True_AVG_Splits_load()
pitcher_stats = pitcher_stats[pitcher_stats['Team'].isin(sp_var1)]
#pitcher_stats = pitcher_stats.set_index('Player')
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn_r').format(precision=3), use_container_width = True)
if stat_var1 == 'HWSr Splits':
pitcher_stats = HWSr_Splits_load()
pitcher_stats = pitcher_stats[pitcher_stats['Team'].isin(sp_var1)]
#pitcher_stats = pitcher_stats.set_index('Player')
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn_r').format(precision=3), use_container_width = True)
elif stat_var1 == 'Current Slate Stats':
pitcher_stats = SP_Slate_Stats_load()
pitcher_stats = pitcher_stats[pitcher_stats['Team'].isin(sp_var1)]
#pitcher_stats = pitcher_stats.set_index('Player')
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn_r').background_gradient(cmap='RdYlGn', subset='K%').format(SP_format, precision=2), use_container_width = True)
elif stat_var1 == 'Stats vs. RHH':
pitcher_stats = RHH_load()
pitcher_stats = pitcher_stats[pitcher_stats['Team'].isin(sp_var1)]
#pitcher_stats = pitcher_stats.set_index('Names')
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r', subset=['Opp RHH', 'Salary', 'BB%', 'True AVG', 'xSLG', 'xBA', 'Hits', 'Homeruns', 'xHRs', 'xHR/PA']).background_gradient(cmap='RdYlGn', subset='K%').format(SP_format, precision=2), use_container_width = True)
elif stat_var1 == 'Stats vs. LHH':
pitcher_stats = LHH_load()
pitcher_stats = pitcher_stats[pitcher_stats['Team'].isin(sp_var1)]
#pitcher_stats = pitcher_stats.set_index('Names')
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r', subset=['Opp LHH', 'Salary', 'BB%', 'True AVG', 'xSLG', 'xBA', 'Hits', 'Homeruns', 'xHRs', 'xHR/PA']).background_gradient(cmap='RdYlGn', subset='K%').format(SP_format, precision=2), use_container_width = True)
elif stat_var1 == 'Full League Stats':
pitcher_stats = Full_Stats_load()
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').background_gradient(cmap='RdYlGn', subset=['Strikeoutper', 'Strikeouts', 'PA']).format(precision=0).format(precision=3, subset = SP_league_format), use_container_width = True)
elif stat_var1 == 'Full League Stats vs. RHH':
pitcher_stats = Full_RHH_load()
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').background_gradient(cmap='RdYlGn', subset=['Strikeoutper', 'Strikeouts', 'PA']).format(precision=0).format(precision=3, subset = SP_league_format), use_container_width = True)
elif stat_var1 == 'Full League Stats vs. LHH':
pitcher_stats = Full_LHH_load()
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').background_gradient(cmap='RdYlGn', subset=['Strikeoutper', 'Strikeouts', 'PA']).format(precision=0).format(precision=3, subset = SP_league_format), use_container_width = True)
elif stat_var1 == 'Bullpen Data':
pitcher_stats = Bullpen_Data_load()
pitcher_stats = pitcher_stats[pitcher_stats['Names'].isin(sp_var1)]
#pitcher_stats = pitcher_stats.set_index('Names')
st.dataframe(pitcher_stats.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').background_gradient(cmap='RdYlGn', subset=['Strikeoutper', 'Strikeouts', 'PA']).format(precision=0).format(precision=3, subset = BP_league_format), use_container_width = True)
elif stat_type_var1 == 'Hitters':
if stat_var1 == 'Current Slate Player Stats':
if site_var1 == 'Draftkings':
hitter_stats = load_table(data_hold, 'DK_Slate_hitters')
if pos_var1 != 'All':
hitter_stats = hitter_stats[hitter_stats['Position'].str.contains('|'.join(pos_var1))]
elif site_var1 == 'Fanduel':
hitter_stats = load_table(data_hold, 'FD_Slate_Hitters')
if pos_var1 != 'All':
hitter_stats = hitter_stats[hitter_stats['Position'].str.contains('|'.join(pos_var1))]
hitter_stats.apply(pd.to_numeric, errors='ignore')
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Order'])
hitter_stats = hitter_stats.dropna(subset=['Opp_SP'])
hitter_stats = hitter_stats.drop(columns=['IBB'])
hitter_stats = hitter_stats.sort_values(by='Event/PA', ascending=False)
hitter_stats = hitter_stats.set_index('Player')
hitter_stats = hitter_stats[hitter_stats['Team'].isin(team_var1)]
st.dataframe(hitter_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['K%', 'Order', 'Salary']).format(hitter_format, precision=0).format(precision=3, subset = ['xBA', 'xSLG']), use_container_width = True)
elif stat_var1 == 'Current Slate Team Stats':
if site_var1 == 'Draftkings':
hitter_stats = load_table(data_hold, 'DK_Slate_Teams')
elif site_var1 == 'Fanduel':
hitter_stats = load_table(data_hold, 'FD_Slate_Teams')
hitter_stats.apply(pd.to_numeric, errors='ignore')
hitter_stats['Acro'] = hitter_stats['Team']
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Opp_SP'])
hitter_stats = hitter_stats.sort_values(by='Event/PA', ascending=False)
hitter_stats = hitter_stats.set_index('Team')
hitter_stats = hitter_stats[hitter_stats['Acro'].isin(team_var1)]
st.dataframe(hitter_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['K%', 'Avg Salary']).format(hitter_format, precision=0).format(precision=3, subset = ['xBA', 'xSLG', 'Opp True AVG']), use_container_width = True)
elif stat_var1 == 'Team Trending Stats (Offense)':
hitter_stats = load_table(data_hold, 'Macro_Trending')
hitter_stats.apply(pd.to_numeric, errors='ignore')
for checkVar in range(len(wrong_acro)):
hitter_stats['Team'] = hitter_stats['Team'].replace(wrong_acro, right_acro)
hitter_stats['Acro'] = hitter_stats['Team']
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Opp'])
hitter_stats = hitter_stats[['Team', 'Opp', 'Avg Score', 'Avg Score L5', 'Avg Score L10', 'Trending Score', '8+ For', '8+ For L5', '8+ For L10', 'Trending 8+ For', 'Acro']]
hitter_stats = hitter_stats.sort_values(by='Trending Score', ascending=False)
hitter_stats = hitter_stats.set_index('Team')
hitter_stats = hitter_stats[hitter_stats['Acro'].isin(team_var1)]
st.dataframe(hitter_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').format(offense_format, precision=2), height=1200, use_container_width = True)
elif stat_var1 == 'Team Trending Stats (Defense)':
hitter_stats = load_table(data_hold, 'Macro_Trending')
hitter_stats.apply(pd.to_numeric, errors='ignore')
for checkVar in range(len(wrong_acro)):
hitter_stats['Team'] = hitter_stats['Team'].replace(wrong_acro, right_acro)
hitter_stats['Acro'] = hitter_stats['Team']
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Opp'])
hitter_stats = hitter_stats[['Team', 'Opp', 'Avg Allowed', 'Avg Allowed L5', 'Avg Allowed L10', 'Trending Avg Allowed', '8+ Allowed', '8+ Allowed L5', '8+ Allowed L10', 'Trending 8+ Allowed', 'Acro']]
hitter_stats = hitter_stats.sort_values(by='Trending Avg Allowed', ascending=False)
hitter_stats = hitter_stats.set_index('Team')
hitter_stats = hitter_stats[hitter_stats['Acro'].isin(team_var1)]
st.dataframe(hitter_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').format(defense_format, precision=2), height=1200, use_container_width = True)
elif stat_var1 == 'Team Trending Stats (Matchup ELO)':
hitter_stats = load_table(data_hold, 'Macro_Trending')
hitter_stats.apply(pd.to_numeric, errors='ignore')
for checkVar in range(len(wrong_acro)):
hitter_stats['Team'] = hitter_stats['Team'].replace(wrong_acro, right_acro)
hitter_stats['Acro'] = hitter_stats['Team']
hitter_stats.replace('', np.nan, inplace=True)
hitter_stats = hitter_stats.dropna(subset=['Opp'])
hitter_stats = hitter_stats[['Team', 'Opp', 'Avg Score', 'Avg Score L5', 'Avg Score L10', 'Trending Score', 'R2_to_Opp_szn', 'R2_to_Opp_sample', 'R2_to_Opp L5', 'R2_to_Opp L10', 'R2_to_Opp_Trend', 'Acro']]
hitter_stats = hitter_stats.sort_values(by='R2_to_Opp_Trend', ascending=False)
hitter_stats = hitter_stats.set_index('Team')
hitter_stats = hitter_stats[hitter_stats['Acro'].isin(team_var1)]
st.dataframe(hitter_stats.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').format(R2_format, precision=2), height=1200, use_container_width = True)
if stat_type_var1 == 'Pitchers':
st.download_button(
label="Export Tables",
data=convert_df_to_csv(pitcher_stats),
file_name='MLB_Research_export.csv',
mime='text/csv',
)
elif stat_type_var1 == 'Hitters':
st.download_button(
label="Export Tables",
data=convert_df_to_csv(hitter_stats),
file_name='MLB_Research_export.csv',
mime='text/csv',
) |