|
import streamlit as st |
|
st.set_page_config(layout="wide") |
|
import numpy as np |
|
import pandas as pd |
|
import time |
|
from fuzzywuzzy import process |
|
import random |
|
|
|
|
|
from global_func.clean_player_name import clean_player_name |
|
from global_func.load_file import load_file |
|
from global_func.load_ss_file import load_ss_file |
|
from global_func.find_name_mismatches import find_name_mismatches |
|
from global_func.predict_dupes import predict_dupes |
|
from global_func.highlight_rows import highlight_changes, highlight_changes_winners, highlight_changes_losers |
|
from global_func.load_csv import load_csv |
|
from global_func.find_csv_mismatches import find_csv_mismatches |
|
|
|
freq_format = {'Finish_percentile': '{:.2%}', 'Lineup Edge': '{:.2%}', 'Win%': '{:.2%}'} |
|
player_wrong_names_mlb = ['Enrique Hernandez'] |
|
player_right_names_mlb = ['Kike Hernandez'] |
|
|
|
tab1, tab2 = st.tabs(["Data Load", "Contest Analysis"]) |
|
with tab1: |
|
if st.button('Clear data', key='reset1'): |
|
st.session_state.clear() |
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
with col1: |
|
st.subheader("Draftkings/Fanduel CSV") |
|
st.info("Upload the player pricing CSV from the site you are playing on.") |
|
|
|
upload_csv_col, csv_template_col = st.columns([3, 1]) |
|
with upload_csv_col: |
|
csv_file = st.file_uploader("Upload CSV File", type=['csv']) |
|
if 'csv_file' in st.session_state: |
|
del st.session_state['csv_file'] |
|
with csv_template_col: |
|
|
|
csv_template_df = pd.DataFrame(columns=['Name', 'ID', 'Roster Position', 'Salary']) |
|
|
|
st.download_button( |
|
label="CSV Template", |
|
data=csv_template_df.to_csv(index=False), |
|
file_name="csv_template.csv", |
|
mime="text/csv" |
|
) |
|
st.session_state['csv_file'] = load_csv(csv_file) |
|
try: |
|
st.session_state['csv_file']['Salary'] = st.session_state['csv_file']['Salary'].astype(str).str.replace(',', '').astype(int) |
|
except: |
|
pass |
|
|
|
if csv_file: |
|
st.session_state['csv_file'] = st.session_state['csv_file'].drop_duplicates(subset=['Name']) |
|
st.success('Projections file loaded successfully!') |
|
st.dataframe(st.session_state['csv_file'].head(10)) |
|
|
|
with col2: |
|
st.subheader("Portfolio File") |
|
st.info("Go ahead and upload a portfolio file here. Only include player columns and an optional 'Stack' column if you are playing MLB.") |
|
saber_toggle = st.radio("Are you uploading from SaberSim?", options=['No', 'Yes']) |
|
st.info("If you are uploading from SaberSim, you will need to upload a CSV file for the slate for name matching.") |
|
if saber_toggle == 'Yes': |
|
if csv_file is not None: |
|
portfolio_file = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) |
|
if 'portfolio' in st.session_state: |
|
del st.session_state['portfolio'] |
|
if 'export_portfolio' in st.session_state: |
|
del st.session_state['export_portfolio'] |
|
|
|
else: |
|
portfolio_file = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) |
|
if 'portfolio' in st.session_state: |
|
del st.session_state['portfolio'] |
|
if 'export_portfolio' in st.session_state: |
|
del st.session_state['export_portfolio'] |
|
|
|
if portfolio_file: |
|
if saber_toggle == 'Yes': |
|
st.session_state['export_portfolio'], st.session_state['portfolio'] = load_ss_file(portfolio_file, st.session_state['csv_file']) |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all') |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True) |
|
st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True) |
|
else: |
|
st.session_state['export_portfolio'], st.session_state['portfolio'] = load_file(portfolio_file) |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all') |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True) |
|
st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True) |
|
|
|
if 'Stack' in st.session_state['portfolio'].columns: |
|
|
|
stack_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Stack'])) |
|
st.write(f"Found {len(stack_dict)} stack assignments") |
|
st.session_state['portfolio'] = st.session_state['portfolio'].drop(columns=['Stack']) |
|
else: |
|
stack_dict = None |
|
st.info("No Stack column found in portfolio") |
|
if st.session_state['portfolio'] is not None: |
|
st.success('Portfolio file loaded successfully!') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb)) |
|
st.dataframe(st.session_state['portfolio'].head(10)) |
|
|
|
with col3: |
|
st.subheader("Projections File") |
|
st.info("upload a projections file that has 'player_names', 'salary', 'median', 'ownership', and 'captain ownership' (Needed for Showdown) columns. Note that the salary for showdown needs to be the FLEX salary, not the captain salary.") |
|
|
|
|
|
upload_col, template_col = st.columns([3, 1]) |
|
|
|
with upload_col: |
|
projections_file = st.file_uploader("Upload Projections File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) |
|
if 'projections_df' in st.session_state: |
|
del st.session_state['projections_df'] |
|
|
|
with template_col: |
|
|
|
template_df = pd.DataFrame(columns=['player_names', 'position', 'team', 'salary', 'median', 'ownership', 'captain ownership']) |
|
|
|
st.download_button( |
|
label="Template", |
|
data=template_df.to_csv(index=False), |
|
file_name="projections_template.csv", |
|
mime="text/csv" |
|
) |
|
|
|
if projections_file: |
|
export_projections, projections = load_file(projections_file) |
|
if projections is not None: |
|
st.success('Projections file loaded successfully!') |
|
projections = projections.apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb)) |
|
st.dataframe(projections.head(10)) |
|
|
|
if portfolio_file and projections_file: |
|
if st.session_state['portfolio'] is not None and projections is not None: |
|
st.subheader("Name Matching Analysis") |
|
|
|
if 'projections_df' not in st.session_state: |
|
st.session_state['projections_df'] = projections.copy() |
|
st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int)) |
|
|
|
|
|
st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df']) |
|
if csv_file is not None and 'export_dict' not in st.session_state: |
|
|
|
try: |
|
name_id_map = dict(zip( |
|
st.session_state['csv_file']['Name'], |
|
st.session_state['csv_file']['Name + ID'] |
|
)) |
|
except: |
|
name_id_map = dict(zip( |
|
st.session_state['csv_file']['Nickname'], |
|
st.session_state['csv_file']['Id'] |
|
)) |
|
|
|
|
|
def find_best_match(name): |
|
best_match = process.extractOne(name, name_id_map.keys()) |
|
if best_match and best_match[1] >= 85: |
|
return name_id_map[best_match[0]] |
|
return name |
|
|
|
|
|
projections['upload_match'] = projections['player_names'].apply(find_best_match) |
|
st.session_state['export_dict'] = dict(zip(projections['player_names'], projections['upload_match'])) |
|
|
|
with tab2: |
|
if st.button('Clear data', key='reset3'): |
|
st.session_state.clear() |
|
if 'portfolio' in st.session_state and 'projections_df' in st.session_state: |
|
col1, col2, col3 = st.columns([1, 8, 1]) |
|
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Win%', 'Lineup Edge'] |
|
with col1: |
|
site_var = st.selectbox("Select Site", ['Draftkings', 'Fanduel']) |
|
sport_var = st.selectbox("Select Sport", ['NFL', 'MLB', 'NBA', 'NHL', 'MMA']) |
|
st.info("It currently does not matter what sport you select, it may matter in the future.") |
|
type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown']) |
|
Contest_Size = st.number_input("Enter Contest Size", value=25000, min_value=1, step=1) |
|
strength_var = st.selectbox("Select field strength", ['Average', 'Sharp', 'Weak']) |
|
if site_var == 'Draftkings': |
|
if type_var == 'Classic': |
|
map_dict = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
elif type_var == 'Showdown': |
|
if sport_var == 'NFL': |
|
map_dict = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'] * 1.5)), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
elif sport_var != 'NFL': |
|
map_dict = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'] / 1.5)), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
elif site_var == 'Fanduel': |
|
map_dict = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
|
|
if type_var == 'Classic': |
|
st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1) |
|
st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1) |
|
st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1) |
|
if stack_dict is not None: |
|
st.session_state['portfolio']['Stack'] = st.session_state['portfolio'].index.map(stack_dict) |
|
elif type_var == 'Showdown': |
|
|
|
st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply( |
|
lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) + |
|
sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
|
|
st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply( |
|
lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) + |
|
sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
|
|
st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply( |
|
lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) + |
|
sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
with col3: |
|
with st.form(key='filter_form'): |
|
max_dupes = st.number_input("Max acceptable dupes?", value=1000, min_value=1, step=1) |
|
min_salary = st.number_input("Min acceptable salary?", value=1000, min_value=1000, step=100) |
|
max_salary = st.number_input("Max acceptable salary?", value=60000, min_value=1000, step=100) |
|
max_finish_percentile = st.number_input("Max acceptable finish percentile?", value=.50, min_value=0.005, step=.001) |
|
player_names = set() |
|
for col in st.session_state['portfolio'].columns: |
|
if col not in excluded_cols: |
|
player_names.update(st.session_state['portfolio'][col].unique()) |
|
player_lock = st.multiselect("Lock players?", options=sorted(list(player_names)), default=[]) |
|
player_remove = st.multiselect("Remove players?", options=sorted(list(player_names)), default=[]) |
|
if stack_dict is not None: |
|
stack_toggle = st.selectbox("Include specific stacks?", options=['All Stacks', 'Specific Stacks'], index=0) |
|
stack_selections = st.multiselect("If Specific Stacks, Which to include?", options=sorted(list(set(stack_dict.values()))), default=[]) |
|
stack_remove = st.multiselect("If Specific Stacks, Which to remove?", options=sorted(list(set(stack_dict.values()))), default=[]) |
|
|
|
submitted = st.form_submit_button("Submit") |
|
|
|
with col2: |
|
st.session_state['portfolio'] = predict_dupes(st.session_state['portfolio'], map_dict, site_var, type_var, Contest_Size, strength_var) |
|
st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['Dupes'] <= max_dupes] |
|
st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['salary'] >= min_salary] |
|
st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['salary'] <= max_salary] |
|
st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['Finish_percentile'] <= max_finish_percentile] |
|
if stack_dict is not None: |
|
if stack_toggle == 'All Stacks': |
|
st.session_state['portfolio'] = st.session_state['portfolio'] |
|
st.session_state['portfolio'] = st.session_state['portfolio'][~st.session_state['portfolio']['Stack'].isin(stack_remove)] |
|
else: |
|
st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['Stack'].isin(stack_selections)] |
|
st.session_state['portfolio'] = st.session_state['portfolio'][~st.session_state['portfolio']['Stack'].isin(stack_remove)] |
|
if player_remove: |
|
|
|
player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols] |
|
remove_mask = st.session_state['portfolio'][player_columns].apply( |
|
lambda row: not any(player in list(row) for player in player_remove), axis=1 |
|
) |
|
st.session_state['portfolio'] = st.session_state['portfolio'][remove_mask] |
|
|
|
if player_lock: |
|
|
|
player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols] |
|
|
|
lock_mask = st.session_state['portfolio'][player_columns].apply( |
|
lambda row: all(player in list(row) for player in player_lock), axis=1 |
|
) |
|
st.session_state['portfolio'] = st.session_state['portfolio'][lock_mask] |
|
export_file = st.session_state['portfolio'].copy() |
|
st.session_state['portfolio'] = st.session_state['portfolio'].sort_values(by='median', ascending=False) |
|
if csv_file is not None: |
|
player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols] |
|
for col in player_columns: |
|
export_file[col] = export_file[col].map(st.session_state['export_dict']) |
|
with st.expander("Download options"): |
|
if stack_dict is not None: |
|
with st.form(key='stack_form'): |
|
st.subheader("Stack Count Adjustments") |
|
st.info("This allows you to fine tune the stacks that you wish to export. If you want to make sure you don't export any of a specific stack you can 0 it out.") |
|
|
|
sort_container = st.container() |
|
with sort_container: |
|
sort_var = st.selectbox("Sort export portfolio by:", options=['median', 'Lineup Edge', 'Own']) |
|
|
|
|
|
unique_stacks = sorted(list(set(stack_dict.values()))) |
|
|
|
|
|
if 'stack_multipliers' not in st.session_state: |
|
st.session_state.stack_multipliers = {stack: 0.0 for stack in unique_stacks} |
|
|
|
|
|
num_cols = 6 |
|
for i in range(0, len(unique_stacks), num_cols): |
|
cols = st.columns(num_cols) |
|
for j, stack in enumerate(unique_stacks[i:i+num_cols]): |
|
with cols[j]: |
|
|
|
key = f"stack_count_{stack}" |
|
|
|
current_stack_count = len(st.session_state['portfolio'][st.session_state['portfolio']['Stack'] == stack]) |
|
|
|
st.session_state.stack_multipliers[stack] = st.number_input( |
|
f"{stack} count", |
|
min_value=0.0, |
|
max_value=float(current_stack_count), |
|
value=float(current_stack_count), |
|
step=1.0, |
|
key=key |
|
) |
|
|
|
|
|
portfolio_copy = st.session_state['portfolio'].copy() |
|
|
|
|
|
selected_rows = [] |
|
|
|
|
|
for stack in unique_stacks: |
|
if stack in st.session_state.stack_multipliers: |
|
count = int(st.session_state.stack_multipliers[stack]) |
|
|
|
stack_rows = portfolio_copy[portfolio_copy['Stack'] == stack] |
|
|
|
top_rows = stack_rows.nlargest(count, sort_var) |
|
selected_rows.append(top_rows) |
|
|
|
|
|
portfolio_copy = pd.concat(selected_rows) |
|
|
|
|
|
export_file = portfolio_copy.copy() |
|
|
|
submitted = st.form_submit_button("Submit") |
|
if submitted: |
|
st.write('Export portfolio updated!') |
|
|
|
st.download_button(label="Download Portfolio", data=export_file.to_csv(index=False), file_name="portfolio.csv", mime="text/csv") |
|
|
|
st.dataframe( |
|
st.session_state['portfolio'].style |
|
.background_gradient(axis=0) |
|
.background_gradient(cmap='RdYlGn') |
|
.background_gradient(cmap='RdYlGn_r', subset=['Finish_percentile', 'Own', 'Dupes']) |
|
.format(freq_format, precision=2), |
|
height=1000, |
|
use_container_width=True |
|
) |
|
|
|
|
|
total_rows = len(st.session_state['portfolio']) |
|
rows_per_page = 500 |
|
total_pages = (total_rows + rows_per_page - 1) // rows_per_page |
|
|
|
|
|
if 'current_page' not in st.session_state: |
|
st.session_state.current_page = 1 |
|
|
|
|
|
st.write( |
|
f"Showing rows {(st.session_state.current_page - 1) * rows_per_page + 1} " |
|
f"to {min(st.session_state.current_page * rows_per_page, total_rows)} of {total_rows}" |
|
) |
|
|
|
|
|
st.session_state.current_page = st.number_input( |
|
f"Page (1-{total_pages})", |
|
min_value=1, |
|
max_value=total_pages, |
|
value=st.session_state.current_page |
|
) |
|
|
|
|
|
start_idx = (st.session_state.current_page - 1) * rows_per_page |
|
end_idx = min(start_idx + rows_per_page, total_rows) |
|
|
|
|
|
current_page_data = st.session_state['portfolio'].iloc[start_idx:end_idx] |
|
|
|
|
|
player_stats = [] |
|
player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols] |
|
|
|
if type_var == 'Showdown': |
|
|
|
for player in player_names: |
|
|
|
cpt_mask = st.session_state['portfolio'][player_columns[0]] == player |
|
|
|
if cpt_mask.any(): |
|
player_stats.append({ |
|
'Player': f"{player} (CPT)", |
|
'Lineup Count': cpt_mask.sum(), |
|
'Avg Median': st.session_state['portfolio'][cpt_mask]['median'].mean(), |
|
'Avg Own': st.session_state['portfolio'][cpt_mask]['Own'].mean(), |
|
'Avg Dupes': st.session_state['portfolio'][cpt_mask]['Dupes'].mean(), |
|
'Avg Finish %': st.session_state['portfolio'][cpt_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': st.session_state['portfolio'][cpt_mask]['Lineup Edge'].mean(), |
|
}) |
|
|
|
|
|
flex_mask = st.session_state['portfolio'][player_columns[1:]].apply( |
|
lambda row: player in list(row), axis=1 |
|
) |
|
|
|
if flex_mask.any(): |
|
player_stats.append({ |
|
'Player': f"{player} (FLEX)", |
|
'Lineup Count': flex_mask.sum(), |
|
'Avg Median': st.session_state['portfolio'][flex_mask]['median'].mean(), |
|
'Avg Own': st.session_state['portfolio'][flex_mask]['Own'].mean(), |
|
'Avg Dupes': st.session_state['portfolio'][flex_mask]['Dupes'].mean(), |
|
'Avg Finish %': st.session_state['portfolio'][flex_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': st.session_state['portfolio'][flex_mask]['Lineup Edge'].mean(), |
|
}) |
|
else: |
|
|
|
for player in player_names: |
|
player_mask = st.session_state['portfolio'][player_columns].apply( |
|
lambda row: player in list(row), axis=1 |
|
) |
|
|
|
if player_mask.any(): |
|
player_stats.append({ |
|
'Player': player, |
|
'Lineup Count': player_mask.sum(), |
|
'Avg Median': st.session_state['portfolio'][player_mask]['median'].mean(), |
|
'Avg Own': st.session_state['portfolio'][player_mask]['Own'].mean(), |
|
'Avg Dupes': st.session_state['portfolio'][player_mask]['Dupes'].mean(), |
|
'Avg Finish %': st.session_state['portfolio'][player_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': st.session_state['portfolio'][player_mask]['Lineup Edge'].mean(), |
|
}) |
|
|
|
player_summary = pd.DataFrame(player_stats) |
|
player_summary = player_summary.sort_values('Lineup Count', ascending=False) |
|
|
|
st.subheader("Player Summary") |
|
st.dataframe( |
|
player_summary.style |
|
.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Avg Finish %', 'Avg Own', 'Avg Dupes']) |
|
.format({ |
|
'Avg Median': '{:.2f}', |
|
'Avg Own': '{:.2f}', |
|
'Avg Dupes': '{:.2f}', |
|
'Avg Finish %': '{:.2%}', |
|
'Avg Lineup Edge': '{:.2%}' |
|
}), |
|
height=400, |
|
use_container_width=True |
|
) |