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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pulp
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
+
import gspread
|
6 |
+
from itertools import combinations
|
7 |
+
|
8 |
+
scope = ['https://www.googleapis.com/auth/spreadsheets',
|
9 |
+
"https://www.googleapis.com/auth/drive"]
|
10 |
+
|
11 |
+
credentials = {
|
12 |
+
"type": "service_account",
|
13 |
+
"project_id": "sheets-api-connect-378620",
|
14 |
+
"private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9",
|
15 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKa01beXwc88R\nnPZVQTNPVQuBnbwoOfc66gW3547ja/UEyIGAF112dt/VqHprRafkKGmlg55jqJNt\na4zceLKV+wTm7vBu7lDISTJfGzCf2TrxQYNqwMKE2LOjI69dBM8u4Dcb4k0wcp9v\ntW1ZzLVVuwTvmrg7JBH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oGBAMcA\nyH4qdbxX9AYrC1dvsSRvgcnzytMvX05LU0uF6tzGtG0zVlub4ahvpEHCfNuy44UT\nxRWW/oFFaWjjyFxO5sWggpUqNuHEnRopg3QXx22SRRTGbN45li/+QAocTkgsiRh1\nqEcYZsO4mPCsQqAy6E2p6RcK+Xa+omxvSnVhq0x1AoGAKr8GdkCl4CF6rieLMAQ7\nLNBuuoYGaHoh8l5E2uOQpzwxVy/nMBcAv+2+KqHEzHryUv1owOi6pMLv7A9mTFoS\n18B0QRLuz5fSOsVnmldfC9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n",
|
16 |
+
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
|
17 |
+
"client_id": "106625872877651920064",
|
18 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
19 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
20 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
21 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
22 |
+
}
|
23 |
+
|
24 |
+
gc = gspread.service_account_from_dict(credentials)
|
25 |
+
|
26 |
+
st.set_page_config(layout="wide")
|
27 |
+
|
28 |
+
game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
|
29 |
+
'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
|
30 |
+
|
31 |
+
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
|
32 |
+
'4x%': '{:.2%}','GPP%': '{:.2%}'}
|
33 |
+
|
34 |
+
wrong_acro = ['WSH', 'AZ']
|
35 |
+
right_acro = ['WAS', 'ARI']
|
36 |
+
|
37 |
+
dk_player_projections = 'https://docs.google.com/spreadsheets/d/1MdzPFqIT0MFid2IhegWf39VNR8IXUyo_Fb5dolOSt3o/edit#gid=340831852'
|
38 |
+
fd_player_projections = 'https://docs.google.com/spreadsheets/d/1MdzPFqIT0MFid2IhegWf39VNR8IXUyo_Fb5dolOSt3o/edit#gid=340831852'
|
39 |
+
|
40 |
+
secondary_dk_player_projections = 'https://docs.google.com/spreadsheets/d/1lP4t8N7UhjR94MEwPn6powRyLl_cQBDUMSCs6cbL9ms/edit#gid=340831852'
|
41 |
+
secondary_fd_player_projections = 'https://docs.google.com/spreadsheets/d/1lP4t8N7UhjR94MEwPn6powRyLl_cQBDUMSCs6cbL9ms/edit#gid=340831852'
|
42 |
+
|
43 |
+
all_dk_player_projections = 'https://docs.google.com/spreadsheets/d/1f42Ergav8K1VsOLOK9MUn7DM_MLMvv4GR2Fy7EfnZTc/edit#gid=500994479'
|
44 |
+
all_fd_player_projections = 'https://docs.google.com/spreadsheets/d/1f42Ergav8K1VsOLOK9MUn7DM_MLMvv4GR2Fy7EfnZTc/edit#gid=500994479'
|
45 |
+
final_Proj = 0
|
46 |
+
|
47 |
+
@st.cache_data
|
48 |
+
def load_time():
|
49 |
+
sh = gc.open_by_url(dk_player_projections)
|
50 |
+
worksheet = sh.worksheet('Timestamp')
|
51 |
+
raw_stamp = worksheet.acell('a1').value
|
52 |
+
|
53 |
+
t_stamp = f"Last update was at {raw_stamp}"
|
54 |
+
|
55 |
+
return t_stamp
|
56 |
+
|
57 |
+
@st.cache_data
|
58 |
+
def set_slate_teams():
|
59 |
+
sh = gc.open_by_url(all_dk_player_projections)
|
60 |
+
worksheet = sh.worksheet('Site_Info')
|
61 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
62 |
+
|
63 |
+
for checkVar in range(len(wrong_acro)):
|
64 |
+
raw_display['FD Main'] = raw_display['FD Main'].replace(wrong_acro, right_acro)
|
65 |
+
|
66 |
+
for checkVar in range(len(wrong_acro)):
|
67 |
+
raw_display['FD Secondary'] = raw_display['FD Secondary'].replace(wrong_acro, right_acro)
|
68 |
+
|
69 |
+
for checkVar in range(len(wrong_acro)):
|
70 |
+
raw_display['FD Overall'] = raw_display['FD Overall'].replace(wrong_acro, right_acro)
|
71 |
+
|
72 |
+
return raw_display
|
73 |
+
|
74 |
+
@st.cache_data
|
75 |
+
def load_dk_player_projections(URL):
|
76 |
+
sh = gc.open_by_url(URL)
|
77 |
+
worksheet = sh.worksheet('DK_Projections')
|
78 |
+
load_display = pd.DataFrame(worksheet.get_all_records())
|
79 |
+
load_display.replace('', np.nan, inplace=True)
|
80 |
+
load_display = load_display.drop_duplicates(subset='Player')
|
81 |
+
raw_display = load_display.dropna(subset=['Median'])
|
82 |
+
|
83 |
+
return raw_display
|
84 |
+
|
85 |
+
@st.cache_data
|
86 |
+
def load_fd_player_projections(URL):
|
87 |
+
sh = gc.open_by_url(URL)
|
88 |
+
worksheet = sh.worksheet('FD_Projections')
|
89 |
+
load_display = pd.DataFrame(worksheet.get_all_records())
|
90 |
+
load_display.replace('', np.nan, inplace=True)
|
91 |
+
load_display = load_display.drop_duplicates(subset='Player')
|
92 |
+
raw_display = load_display.dropna(subset=['Median'])
|
93 |
+
|
94 |
+
return raw_display
|
95 |
+
|
96 |
+
@st.cache_data
|
97 |
+
def load_dk_player_roo(URL):
|
98 |
+
sh = gc.open_by_url(URL)
|
99 |
+
worksheet = sh.worksheet('Player_ROO')
|
100 |
+
load_display = pd.DataFrame(worksheet.get_all_records())
|
101 |
+
raw_display = load_display
|
102 |
+
|
103 |
+
return raw_display
|
104 |
+
|
105 |
+
@st.cache_data
|
106 |
+
def load_fd_player_roo(URL):
|
107 |
+
sh = gc.open_by_url(URL)
|
108 |
+
worksheet = sh.worksheet('FD_Player_ROO')
|
109 |
+
load_display = pd.DataFrame(worksheet.get_all_records())
|
110 |
+
raw_display = load_display
|
111 |
+
|
112 |
+
return raw_display
|
113 |
+
|
114 |
+
@st.cache_data
|
115 |
+
def convert_df_to_csv(df):
|
116 |
+
return df.to_csv().encode('utf-8')
|
117 |
+
|
118 |
+
t_stamp = load_time()
|
119 |
+
site_slates = set_slate_teams()
|
120 |
+
col1, col2 = st.columns([1, 5])
|
121 |
+
|
122 |
+
with col1:
|
123 |
+
#st.info(t_stamp)
|
124 |
+
if st.button("Load/Reset Data", key='reset3'):
|
125 |
+
t_stamp = load_time()
|
126 |
+
st.cache_data.clear()
|
127 |
+
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Secondary Slate', 'All Games'), key='slate_var1')
|
128 |
+
site_var1 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var1')
|
129 |
+
custom_var1 = st.radio("Are you creating a custom table?", ('No', 'Yes'), key='custom_var1')
|
130 |
+
if custom_var1 == 'No':
|
131 |
+
if slate_var1 == 'Main Slate':
|
132 |
+
if site_var1 == 'Draftkings':
|
133 |
+
slate_teams = site_slates['DK Main'].values.tolist()
|
134 |
+
raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
135 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
136 |
+
elif site_var1 == 'Fanduel':
|
137 |
+
slate_teams = site_slates['FD Main'].values.tolist()
|
138 |
+
raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
139 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
140 |
+
elif slate_var1 == 'Secondary Slate':
|
141 |
+
if site_var1 == 'Draftkings':
|
142 |
+
slate_teams = site_slates['DK Secondary'].values.tolist()
|
143 |
+
raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
144 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
145 |
+
elif site_var1 == 'Fanduel':
|
146 |
+
slate_teams = site_slates['FD Secondary'].values.tolist()
|
147 |
+
raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
148 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
149 |
+
elif slate_var1 == 'All Games':
|
150 |
+
if site_var1 == 'Draftkings':
|
151 |
+
slate_teams = site_slates['DK Overall'].values.tolist()
|
152 |
+
raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
153 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
154 |
+
elif site_var1 == 'Fanduel':
|
155 |
+
slate_teams = site_slates['FD Overall'].values.tolist()
|
156 |
+
raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
157 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
158 |
+
split_var1 = st.radio("Would you like to view the whole slate or just specific games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
|
159 |
+
if split_var1 == 'Specific Games':
|
160 |
+
team_var1 = st.multiselect('Which teams would you like to include in the ROO?', options = raw_baselines['Team'].unique(), key='team_var1')
|
161 |
+
elif split_var1 == 'Full Slate Run':
|
162 |
+
team_var1 = raw_baselines.Team.values.tolist()
|
163 |
+
pos_split1 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1')
|
164 |
+
if pos_split1 == 'Specific Positions':
|
165 |
+
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['SP', 'P', 'C', '1B', '2B', '3B', 'SS', 'OF'])
|
166 |
+
elif pos_split1 == 'All Positions':
|
167 |
+
pos_var1 = 'All'
|
168 |
+
if custom_var1 == 'Yes':
|
169 |
+
contest_var1 = st.selectbox("What contest type are you running for?", ('Cash', 'Small Field GPP', 'Large Field GPP'), key='contest_var1')
|
170 |
+
if slate_var1 == 'Main Slate':
|
171 |
+
if site_var1 == 'Draftkings':
|
172 |
+
slate_teams = site_slates['DK Main'].values.tolist()
|
173 |
+
raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
174 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
175 |
+
elif site_var1 == 'Fanduel':
|
176 |
+
slate_teams = site_slates['FD Main'].values.tolist()
|
177 |
+
raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
178 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
179 |
+
elif slate_var1 == 'Secondary Slate':
|
180 |
+
if site_var1 == 'Draftkings':
|
181 |
+
slate_teams = site_slates['DK Secondary'].values.tolist()
|
182 |
+
raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
183 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
184 |
+
elif site_var1 == 'Fanduel':
|
185 |
+
slate_teams = site_slates['FD Secondary'].values.tolist()
|
186 |
+
raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
187 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
188 |
+
elif slate_var1 == 'All Games':
|
189 |
+
if site_var1 == 'Draftkings':
|
190 |
+
slate_teams = site_slates['DK Overall'].values.tolist()
|
191 |
+
raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
192 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
193 |
+
elif site_var1 == 'Fanduel':
|
194 |
+
slate_teams = site_slates['FD Overall'].values.tolist()
|
195 |
+
raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
196 |
+
raw_baselines = raw_baselines[raw_baselines['Team'].isin(slate_teams)]
|
197 |
+
split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
|
198 |
+
if split_var1 == 'Specific Games':
|
199 |
+
team_var1 = st.multiselect('Which teams would you like to include in the ROO?', options = raw_baselines['Team'].unique(), key='team_var1')
|
200 |
+
elif split_var1 == 'Full Slate Run':
|
201 |
+
team_var1 = raw_baselines.Team.values.tolist()
|
202 |
+
pos_split1 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1')
|
203 |
+
if pos_split1 == 'Specific Positions':
|
204 |
+
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['SP', 'P', 'C', '1B', '2B', '3B', 'SS', 'OF'])
|
205 |
+
elif pos_split1 == 'All Positions':
|
206 |
+
pos_var1 = 'All'
|
207 |
+
|
208 |
+
|
209 |
+
with col2:
|
210 |
+
if custom_var1 == 'No':
|
211 |
+
if slate_var1 == 'Main Slate':
|
212 |
+
if site_var1 == 'Draftkings':
|
213 |
+
final_Proj = load_dk_player_roo(dk_player_projections)
|
214 |
+
elif site_var1 == 'Fanduel':
|
215 |
+
final_Proj = load_fd_player_roo(fd_player_projections)
|
216 |
+
elif slate_var1 == 'Secondary Slate':
|
217 |
+
if site_var1 == 'Draftkings':
|
218 |
+
final_Proj = load_dk_player_roo(secondary_dk_player_projections)
|
219 |
+
elif site_var1 == 'Fanduel':
|
220 |
+
final_Proj = load_fd_player_roo(secondary_fd_player_projections)
|
221 |
+
elif slate_var1 == 'All Games':
|
222 |
+
if site_var1 == 'Draftkings':
|
223 |
+
final_Proj = load_dk_player_roo(all_dk_player_projections)
|
224 |
+
elif site_var1 == 'Fanduel':
|
225 |
+
final_Proj = load_fd_player_roo(all_fd_player_projections)
|
226 |
+
final_Proj = final_Proj[final_Proj['Team'].isin(team_var1)]
|
227 |
+
if pos_var1 != 'All':
|
228 |
+
final_Proj = final_Proj[final_Proj['Position'].str.contains('|'.join(pos_var1))]
|
229 |
+
st.dataframe(final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), use_container_width = True)
|
230 |
+
st.download_button(
|
231 |
+
label="Export Tables",
|
232 |
+
data=convert_df_to_csv(final_Proj),
|
233 |
+
file_name='Custom_MLB_export.csv',
|
234 |
+
mime='text/csv',
|
235 |
+
)
|
236 |
+
elif custom_var1 == 'Yes':
|
237 |
+
hold_container = st.empty()
|
238 |
+
if st.button('Create Range of Outcomes for Slate'):
|
239 |
+
with hold_container:
|
240 |
+
# if slate_var1 == 'Main Slate':
|
241 |
+
# if site_var1 == 'Draftkings':
|
242 |
+
# raw_baselines = load_dk_player_projections(dk_player_projections)
|
243 |
+
# elif site_var1 == 'Fanduel':
|
244 |
+
# raw_baselines = load_fd_player_projections(fd_player_projections)
|
245 |
+
# elif slate_var1 == 'Secondary Slate':
|
246 |
+
# if site_var1 == 'Draftkings':
|
247 |
+
# raw_baselines = load_dk_player_projections(secondary_dk_player_projections)
|
248 |
+
# elif site_var1 == 'Fanduel':
|
249 |
+
# raw_baselines = load_fd_player_projections(secondary_fd_player_projections)
|
250 |
+
# elif slate_var1 == 'All Games':
|
251 |
+
# if site_var1 == 'Draftkings':
|
252 |
+
# raw_baselines = load_dk_player_projections(all_dk_player_projections)
|
253 |
+
# elif site_var1 == 'Fanduel':
|
254 |
+
# raw_baselines = load_fd_player_projections(all_fd_player_projections)
|
255 |
+
working_roo = raw_baselines
|
256 |
+
working_roo = working_roo[working_roo['Team'].isin(team_var1)]
|
257 |
+
own_dict = dict(zip(working_roo.Player, working_roo.Own))
|
258 |
+
team_dict = dict(zip(working_roo.Player, working_roo.Team))
|
259 |
+
total_sims = 1000
|
260 |
+
|
261 |
+
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median', 'Ceiling_Var']]
|
262 |
+
flex_file['Floor'] = flex_file['Median']*.25
|
263 |
+
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'SP', (flex_file['Median'] + (flex_file['Floor'])) + ((flex_file['Ceiling_Var'] * 10) * 3), (flex_file['Median'] + (flex_file['Floor'])) + ((flex_file['Ceiling_Var'] * 10)))
|
264 |
+
flex_file['STD'] = (flex_file['Median']/4)
|
265 |
+
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
266 |
+
if pos_split1 == 'All Positions':
|
267 |
+
flex_file = flex_file
|
268 |
+
elif pos_split1 != 'All Positions':
|
269 |
+
if pos_var1 == 'Pitchers':
|
270 |
+
flex_file = flex_file[flex_file['Position'] == 'SP']
|
271 |
+
elif pos_var1 == 'Hitters':
|
272 |
+
flex_file = flex_file[flex_file['Position'] != 'SP']
|
273 |
+
elif pos_var1 not in ['Pitchers', 'Hitters']:
|
274 |
+
flex_file = flex_file[flex_file['Position'].str.contains('|'.join(pos_var1))]
|
275 |
+
hold_file = flex_file
|
276 |
+
overall_file = flex_file
|
277 |
+
salary_file = flex_file
|
278 |
+
|
279 |
+
overall_players = overall_file[['Player']]
|
280 |
+
|
281 |
+
for x in range(0,total_sims):
|
282 |
+
salary_file[x] = salary_file['Salary']
|
283 |
+
|
284 |
+
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
285 |
+
salary_file.astype('int').dtypes
|
286 |
+
|
287 |
+
salary_file = salary_file.div(1000)
|
288 |
+
|
289 |
+
for x in range(0,total_sims):
|
290 |
+
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
291 |
+
|
292 |
+
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
293 |
+
overall_file.astype('int').dtypes
|
294 |
+
|
295 |
+
players_only = hold_file[['Player']]
|
296 |
+
raw_lineups_file = players_only
|
297 |
+
|
298 |
+
for x in range(0,total_sims):
|
299 |
+
maps_dict = {'proj_map':dict(zip(hold_file.Player,hold_file[x]))}
|
300 |
+
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
301 |
+
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
302 |
+
|
303 |
+
players_only=players_only.drop(['Player'], axis=1)
|
304 |
+
players_only.astype('int').dtypes
|
305 |
+
|
306 |
+
salary_2x_check = (overall_file - (salary_file*2))
|
307 |
+
salary_3x_check = (overall_file - (salary_file*3))
|
308 |
+
salary_4x_check = (overall_file - (salary_file*4))
|
309 |
+
gpp_check = (overall_file - ((salary_file*2)+10))
|
310 |
+
|
311 |
+
players_only['Average_Rank'] = players_only.mean(axis=1)
|
312 |
+
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
313 |
+
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
314 |
+
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
315 |
+
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
316 |
+
players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
317 |
+
players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
318 |
+
players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
319 |
+
players_only['GPP%'] = gpp_check[gpp_check >= 1].count(axis=1)/float(total_sims)
|
320 |
+
|
321 |
+
players_only['Player'] = hold_file[['Player']]
|
322 |
+
|
323 |
+
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%']]
|
324 |
+
|
325 |
+
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
|
326 |
+
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%']]
|
327 |
+
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
|
328 |
+
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
|
329 |
+
final_Proj['Own'] = final_Proj['Own'].astype('float')
|
330 |
+
if contest_var1 == 'Small Field GPP':
|
331 |
+
if site_var1 == 'Draftkings':
|
332 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] == 'SP') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean() >= 0), final_Proj['Own'] * (5 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean(), final_Proj['Own'])
|
333 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] != 'SP') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean() >= 0), final_Proj['Own'] * (10 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean(), final_Proj['Own%'])
|
334 |
+
final_Proj['Own%'] = np.where(final_Proj['Own%'] > 75, 75, final_Proj['Own%'])
|
335 |
+
elif site_var1 == 'Fanduel':
|
336 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] == 'P') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean() >= 0), final_Proj['Own'] * (5 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean(), final_Proj['Own'])
|
337 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] != 'P') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean() >= 0), final_Proj['Own'] * (10 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean())/150) + final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean(), final_Proj['Own%'])
|
338 |
+
final_Proj['Own%'] = np.where(final_Proj['Own%'] > 75, 75, final_Proj['Own%'])
|
339 |
+
elif contest_var1 == 'Large Field GPP':
|
340 |
+
if site_var1 == 'Draftkings':
|
341 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] == 'SP') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean() >= 0), final_Proj['Own'] * (2.5 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean(), final_Proj['Own'])
|
342 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] != 'SP') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean() >= 0), final_Proj['Own'] * (5 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean(), final_Proj['Own%'])
|
343 |
+
final_Proj['Own%'] = np.where(final_Proj['Own%'] > 75, 75, final_Proj['Own%'])
|
344 |
+
elif site_var1 == 'Fanduel':
|
345 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] == 'P') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean() >= 0), final_Proj['Own'] * (2.5 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean(), final_Proj['Own'])
|
346 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] != 'P') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean() >= 0), final_Proj['Own'] * (5 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean())/150) + final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean(), final_Proj['Own%'])
|
347 |
+
final_Proj['Own%'] = np.where(final_Proj['Own%'] > 75, 75, final_Proj['Own%'])
|
348 |
+
elif contest_var1 == 'Cash':
|
349 |
+
if site_var1 == 'Draftkings':
|
350 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] == 'SP') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean() >= 0), final_Proj['Own'] * (6 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] == 'SP', 'Own'].mean(), final_Proj['Own'])
|
351 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] != 'SP') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean() >= 0), final_Proj['Own'] * (11 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] != 'SP', 'Own'].mean(), final_Proj['Own%'])
|
352 |
+
final_Proj['Own%'] = np.where(final_Proj['Own%'] > 75, 75, final_Proj['Own%'])
|
353 |
+
elif site_var1 == 'Fanduel':
|
354 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] == 'P') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean() >= 0), final_Proj['Own'] * (6 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean())/100) + final_Proj.loc[final_Proj['Position'] == 'P', 'Own'].mean(), final_Proj['Own'])
|
355 |
+
final_Proj['Own%'] = np.where((final_Proj['Position'] != 'P') & (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean() >= 0), final_Proj['Own'] * (11 * (final_Proj['Own'] - final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean())/150) + final_Proj.loc[final_Proj['Position'] != 'P', 'Own'].mean(), final_Proj['Own%'])
|
356 |
+
final_Proj['Own%'] = np.where(final_Proj['Own%'] > 75, 75, final_Proj['Own%'])
|
357 |
+
|
358 |
+
final_Proj = final_Proj[['Player', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%', 'Own%']]
|
359 |
+
final_Proj = final_Proj.set_index('Player')
|
360 |
+
final_Proj = final_Proj.sort_values(by='Median', ascending=False)
|
361 |
+
|
362 |
+
with hold_container:
|
363 |
+
hold_container = st.empty()
|
364 |
+
final_Proj = final_Proj
|
365 |
+
st.dataframe(final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), use_container_width = True)
|
366 |
+
|
367 |
+
st.download_button(
|
368 |
+
label="Export Tables",
|
369 |
+
data=convert_df_to_csv(final_Proj),
|
370 |
+
file_name='Custom_MLB_export.csv',
|
371 |
+
mime='text/csv',
|
372 |
+
)
|