File size: 6,291 Bytes
9c7e08b e560f1d 9c7e08b 5c9b782 ab18789 9c7e08b a8cf688 9c7e08b 0dedc97 9c7e08b 0dedc97 9c7e08b 7deab18 0dedc97 c375de1 e560f1d 7deab18 e1f40de ee49c6f a8cf688 433ab29 42712b2 a7f801a 42712b2 53d6dd0 a7f801a 42712b2 53d6dd0 a7f801a 53d6dd0 a7f801a 42712b2 a8cf688 e560f1d 9c7e08b 433ab29 9c7e08b |
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 |
import streamlit as st
import pandas as pd
def load_contest_file(upload, sport):
pos_values = ['P', 'C', '1B', '2B', '3B', 'SS', 'OF']
if upload is not None:
try:
try:
if upload.name.endswith('.csv'):
raw_df = pd.read_csv(upload)
elif upload.name.endswith(('.xls', '.xlsx')):
raw_df = pd.read_excel(upload)
else:
st.error('Please upload either a CSV or Excel file')
return None
except:
raw_df = upload
df = raw_df[['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS', 'Salary', 'Team']]
df = df.rename(columns={'Roster Position': 'Pos', '%Drafted': 'Own'})
# Split EntryName into base name and entry count
df['BaseName'] = df['EntryName'].str.replace(r'\s*\(\d+/\d+\)$', '', regex=True)
df['EntryCount'] = df['EntryName'].str.extract(r'\((\d+/\d+)\)')
df['EntryCount'] = df['EntryCount'].fillna('1/1') # Default to 1/1 if no entry count
# Split the lineup string by replacing position indicators with commas
# We need to ensure we only replace position indicators that are at the start of a player entry
# and not those that might appear within player names
df['Lineup'] = df['Lineup'].str.replace(r'\b(' + '|'.join(pos_values) + r')\b', r'\1,', regex=True)
# Split into individual columns and remove position indicators
# First, determine the maximum number of players in any lineup
max_players = int(df['Lineup'].str.split(',').str.len().max())
if max_players <= 0:
st.error('No valid lineups found in the uploaded file')
return None
# Create columns for each player
for i in range(1, max_players):
df[i] = df['Lineup'].str.split(',').str[i].str.strip()
# Remove position indicators from the end of each entry
df[i] = df[i].str.replace(r'\s+(' + '|'.join(pos_values) + r')$', '', regex=True)
# Replace None with -1
df[i] = df[i].fillna('-1')
if sport == 'MLB':
df = df.rename(columns={1: '1B', 2: '2B', 3: '3B', 4: 'C', 5: 'OF1', 6: 'OF2', 7: 'OF3', 8: 'SP1', 9: 'SP2', 10: 'SS'})
try:
df['Own'] = df['Own'].str.replace('%', '').astype(float)
except:
df['Own'] = df['Own'].astype(float)
ownership_df = df[['Player', 'Own']]
fpts_df = df[['Player', 'FPTS']]
salary_df = df[['Player', 'Salary']]
team_df = df[['Player', 'Team']]
pos_df = df[['Player', 'Pos']]
# Create position mapping dictionary
pos_dict = dict(zip(pos_df['Player'], pos_df['Pos']))
# Debug prints
print("\nPosition Dictionary:")
print(pos_dict)
print("\nSample Lineup String:")
print(df['Lineup'].iloc[0]) # Print first lineup
# Function to check if player is eligible for position
def is_eligible_for_position(player, target_pos):
if player not in pos_dict:
print(f"Player not found in pos_dict: {player}")
return False
player_positions = pos_dict[player].split('/')
print(f"Checking {player} for {target_pos}. Player positions: {player_positions}")
# Handle special cases
if target_pos.startswith('SP') and 'P' in player_positions:
return True
if target_pos.startswith('OF') and 'OF' in player_positions:
return True
return target_pos in player_positions
# Process each lineup
for idx, row in df.iterrows():
# Get all players in the lineup
players = row['Lineup'].split(',')
players = [p.strip() for p in players if p.strip()]
print(f"\nProcessing lineup {idx}:")
print(f"Players found: {players}")
# First pass: fill required positions (excluding OF)
required_positions = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS']
for pos in required_positions:
for player in players:
if is_eligible_for_position(player, pos):
print(f"Assigning {player} to {pos}")
df.at[idx, pos] = player
players.remove(player)
break
else:
print(f"No player found for {pos}")
# Second pass: fill OF positions with remaining players
of_positions = ['OF1', 'OF2', 'OF3']
for pos in of_positions:
for player in players:
if 'OF' in pos_dict.get(player, '').split('/'):
print(f"Assigning {player} to {pos}")
df.at[idx, pos] = player
players.remove(player)
break
else:
print(f"No player found for {pos}, using -1")
df.at[idx, pos] = '-1'
cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS', 'Salary', 'Team'])
cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]
entry_list = list(set(df['BaseName']))
entry_list.sort()
return cleaned_df, ownership_df, fpts_df, salary_df, team_df, pos_df, entry_list
except Exception as e:
st.error(f'Error loading file: {str(e)}')
return None
return None |