File size: 2,654 Bytes
9c7e08b
 
 
e560f1d
de2ad46
245a9b5
9c7e08b
 
5c9b782
 
 
 
 
 
 
 
 
ab18789
9c7e08b
795a6d7
a8cf688
9c7e08b
 
 
 
 
 
 
795a6d7
e1f40de
 
 
 
795a6d7
 
ee49c6f
 
a8cf688
 
433ab29
42712b2
795a6d7
 
7443266
978080b
56ac316
42712b2
795a6d7
9c7e08b
 
 
433ab29
795a6d7
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
import streamlit as st
import pandas as pd

def load_contest_file(upload, sport):
    if sport == 'MLB':
        pos_list = [' 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
            
            # Select and rename essential columns
            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
            
            # Convert ownership percentage to float
            try:
                df['Own'] = df['Own'].str.replace('%', '').astype(float)
            except:
                df['Own'] = df['Own'].astype(float)
            
            # Create separate dataframes for different player attributes
            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 the cleaned dataframe with just the essential columns
            cleaned_df = df[['BaseName', 'EntryCount', 'Lineup']]
            cleaned_df['Lineup'] = cleaned_df['Lineup'].replace(pos_list, value=',', regex=True)
            cleaned_df[['Remove', '1B', '2B', '3B', 'C', 'OF1', 'OF2', 'OF3', 'P1', 'P2', 'SS']] = cleaned_df['Lineup'].str.split(',', expand=True)
            cleaned_df = cleaned_df.drop(columns=['Lineup', 'Remove'])
            
            # Get unique entry names
            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