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', )