James McCool
commited on
Commit
·
ee49c6f
1
Parent(s):
8cb0690
Refactor find_name_mismatches function and update data handling
Browse files- Renamed parameters in the find_name_mismatches function for clarity, changing ownership_dict and fpts_dict to ownership_df and fpts_df.
- Updated the logic to use raw copies of dataframes for processing, ensuring the original data remains unaltered.
- Improved the handling of ownership and fpts updates to maintain data integrity during name matching operations.
global_func/find_name_mismatches.py
CHANGED
|
@@ -4,24 +4,24 @@ import pandas as pd
|
|
| 4 |
import time
|
| 5 |
from fuzzywuzzy import process
|
| 6 |
|
| 7 |
-
def find_name_mismatches(contest_df, projections_df,
|
| 8 |
# Create a copy of the projections dataframe to avoid modifying the original
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
name_columns = [col for col in
|
| 15 |
|
| 16 |
-
if 'player_names' not in
|
| 17 |
st.error("No 'player_names' column found in projections file")
|
| 18 |
-
return
|
| 19 |
|
| 20 |
# Get unique player names from portfolio and projections
|
| 21 |
portfolio_players = set()
|
| 22 |
for col in name_columns:
|
| 23 |
-
portfolio_players.update(
|
| 24 |
-
projection_players = set(
|
| 25 |
portfolio_players_list = list(portfolio_players)
|
| 26 |
projection_players_list = list(projection_players)
|
| 27 |
|
|
@@ -79,44 +79,48 @@ def find_name_mismatches(contest_df, projections_df, ownership_dict, fpts_dict):
|
|
| 79 |
# Process automatic matches
|
| 80 |
for projection_name, contest_name in auto_matches.items():
|
| 81 |
for col in name_columns:
|
| 82 |
-
|
| 83 |
|
| 84 |
-
if contest_name in
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Process manual selections
|
| 90 |
for projection_name, selection in selections.items():
|
| 91 |
if selection != "None of these":
|
| 92 |
selected_name = selection.split(" (")[0]
|
| 93 |
for col in name_columns:
|
| 94 |
-
|
| 95 |
|
| 96 |
-
if
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
st.success(f"Replaced '{selected_name}' with '{projection_name}'")
|
| 102 |
|
| 103 |
st.success("All changes applied successfully!")
|
| 104 |
-
|
| 105 |
-
st.write(st.session_state['ownership_dict'])
|
| 106 |
-
return contest_df, projections_df, ownership_dict, fpts_dict
|
| 107 |
|
| 108 |
# Return the current state if form hasn't been submitted yet
|
| 109 |
-
return
|
| 110 |
else:
|
| 111 |
st.success("All players have been automatically matched!")
|
| 112 |
# Apply automatic matches
|
| 113 |
for projection_name, contest_name in auto_matches.items():
|
| 114 |
for col in name_columns:
|
| 115 |
-
|
| 116 |
|
| 117 |
-
if contest_name in
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
return
|
|
|
|
| 4 |
import time
|
| 5 |
from fuzzywuzzy import process
|
| 6 |
|
| 7 |
+
def find_name_mismatches(contest_df, projections_df, ownership_df, fpts_df):
|
| 8 |
# Create a copy of the projections dataframe to avoid modifying the original
|
| 9 |
+
projections_raw = projections_df.copy()
|
| 10 |
+
contest_raw = contest_df.copy()
|
| 11 |
+
ownership_raw = ownership_df.copy()
|
| 12 |
+
fpts_raw = fpts_df.copy()
|
| 13 |
|
| 14 |
+
name_columns = [col for col in contest_raw.columns if not col in ['BaseName', 'EntryCount']]
|
| 15 |
|
| 16 |
+
if 'player_names' not in projections_raw.columns:
|
| 17 |
st.error("No 'player_names' column found in projections file")
|
| 18 |
+
return contest_raw, projections_raw
|
| 19 |
|
| 20 |
# Get unique player names from portfolio and projections
|
| 21 |
portfolio_players = set()
|
| 22 |
for col in name_columns:
|
| 23 |
+
portfolio_players.update(contest_raw[col].unique())
|
| 24 |
+
projection_players = set(projections_raw['player_names'].unique())
|
| 25 |
portfolio_players_list = list(portfolio_players)
|
| 26 |
projection_players_list = list(projection_players)
|
| 27 |
|
|
|
|
| 79 |
# Process automatic matches
|
| 80 |
for projection_name, contest_name in auto_matches.items():
|
| 81 |
for col in name_columns:
|
| 82 |
+
contest_raw[col] = contest_raw[col].replace(contest_name, projection_name)
|
| 83 |
|
| 84 |
+
if contest_name in ownership_raw:
|
| 85 |
+
ownership_raw['Player'] = ownership_raw['Player'].replace(contest_name, projection_name)
|
| 86 |
+
ownership_dict = dict(zip(ownership_raw['Player'], ownership_raw['Own']))
|
| 87 |
+
if contest_name in fpts_raw:
|
| 88 |
+
fpts_raw['Player'] = fpts_raw['Player'].replace(contest_name, projection_name)
|
| 89 |
+
fpts_dict = dict(zip(fpts_raw['Player'], fpts_raw['FPTS']))
|
| 90 |
|
| 91 |
# Process manual selections
|
| 92 |
for projection_name, selection in selections.items():
|
| 93 |
if selection != "None of these":
|
| 94 |
selected_name = selection.split(" (")[0]
|
| 95 |
for col in name_columns:
|
| 96 |
+
contest_raw[col] = contest_raw[col].replace(selected_name, projection_name)
|
| 97 |
|
| 98 |
+
if contest_name in ownership_raw:
|
| 99 |
+
ownership_raw['Player'] = ownership_raw['Player'].replace(contest_name, projection_name)
|
| 100 |
+
ownership_dict = dict(zip(ownership_raw['Player'], ownership_raw['Own']))
|
| 101 |
+
if contest_name in fpts_raw:
|
| 102 |
+
fpts_raw['Player'] = fpts_raw['Player'].replace(contest_name, projection_name)
|
| 103 |
+
fpts_dict = dict(zip(fpts_raw['Player'], fpts_raw['FPTS']))
|
| 104 |
|
| 105 |
st.success(f"Replaced '{selected_name}' with '{projection_name}'")
|
| 106 |
|
| 107 |
st.success("All changes applied successfully!")
|
| 108 |
+
return contest_raw, projections_raw, ownership_dict, fpts_dict
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# Return the current state if form hasn't been submitted yet
|
| 111 |
+
return contest_raw, projections_raw, ownership_dict, fpts_dict
|
| 112 |
else:
|
| 113 |
st.success("All players have been automatically matched!")
|
| 114 |
# Apply automatic matches
|
| 115 |
for projection_name, contest_name in auto_matches.items():
|
| 116 |
for col in name_columns:
|
| 117 |
+
contest_raw[col] = contest_raw[col].replace(contest_name, projection_name)
|
| 118 |
|
| 119 |
+
if contest_name in ownership_raw:
|
| 120 |
+
ownership_raw['Player'] = ownership_raw['Player'].replace(contest_name, projection_name)
|
| 121 |
+
ownership_dict = dict(zip(ownership_raw['Player'], ownership_raw['Own']))
|
| 122 |
+
if contest_name in fpts_raw:
|
| 123 |
+
fpts_raw['Player'] = fpts_raw['Player'].replace(contest_name, projection_name)
|
| 124 |
+
fpts_dict = dict(zip(fpts_raw['Player'], fpts_raw['FPTS']))
|
| 125 |
|
| 126 |
+
return contest_raw, projections_raw, ownership_dict, fpts_dict
|
global_func/load_contest_file.py
CHANGED
|
@@ -49,14 +49,14 @@ def load_contest_file(upload, sport):
|
|
| 49 |
if sport == 'MLB':
|
| 50 |
df = df.rename(columns={1: '1B', 2: '2B', 3: '3B', 4: 'C', 5: 'OF1', 6: 'OF2', 7: 'OF3', 8: 'SP1', 9: 'SP2', 10: 'SS'})
|
| 51 |
df['Own'] = df['Own'].str.replace('%', '').astype(float)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS'])
|
| 55 |
cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]
|
| 56 |
entry_list = list(set(df['BaseName']))
|
| 57 |
entry_list.sort()
|
| 58 |
|
| 59 |
-
return cleaned_df,
|
| 60 |
except Exception as e:
|
| 61 |
st.error(f'Error loading file: {str(e)}')
|
| 62 |
return None
|
|
|
|
| 49 |
if sport == 'MLB':
|
| 50 |
df = df.rename(columns={1: '1B', 2: '2B', 3: '3B', 4: 'C', 5: 'OF1', 6: 'OF2', 7: 'OF3', 8: 'SP1', 9: 'SP2', 10: 'SS'})
|
| 51 |
df['Own'] = df['Own'].str.replace('%', '').astype(float)
|
| 52 |
+
ownership_df = df[['Player', 'Own']]
|
| 53 |
+
fpts_df = df[['Player', 'FPTS']]
|
| 54 |
cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS'])
|
| 55 |
cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]
|
| 56 |
entry_list = list(set(df['BaseName']))
|
| 57 |
entry_list.sort()
|
| 58 |
|
| 59 |
+
return cleaned_df, ownership_df, fpts_df, entry_list
|
| 60 |
except Exception as e:
|
| 61 |
st.error(f'Error loading file: {str(e)}')
|
| 62 |
return None
|