James McCool commited on
Commit
25703b9
·
1 Parent(s): 9f87d22

Refactor load_contest_file function to enhance player data processing

Browse files

- Updated the logic for splitting player entries to handle empty strings more effectively, ensuring cleaner data output.
- Adjusted the column renaming for MLB to start from index 0, improving consistency with player position assignments.
- These changes contribute to ongoing efforts to improve data integrity and user experience within the application.

Files changed (1) hide show
  1. global_func/load_contest_file.py +6 -5
global_func/load_contest_file.py CHANGED
@@ -26,8 +26,6 @@ def load_contest_file(upload, sport):
26
  df['EntryCount'] = df['EntryCount'].fillna('1/1') # Default to 1/1 if no entry count
27
 
28
  # Split the lineup string by replacing position indicators with commas
29
- # We need to ensure we only replace position indicators that are at the start of a player entry
30
- # and not those that might appear within player names
31
  df['Lineup'] = df['Lineup'].str.replace(r'\b(' + '|'.join(pos_values) + r')\b', r'\1,', regex=True)
32
 
33
  # Split into individual columns and remove position indicators
@@ -39,13 +37,16 @@ def load_contest_file(upload, sport):
39
  return None
40
 
41
  # Create columns for each player
42
- for i in range(1, max_players):
43
- df[i] = df['Lineup'].str.split(',').str[i].str.strip()
 
44
  # Remove position indicators from the end of each entry
45
  df[i] = df[i].str.replace(r'\s+(' + '|'.join(pos_values) + r')$', '', regex=True)
 
 
46
 
47
  if sport == 'MLB':
48
- df = df.rename(columns={1: '1B', 2: '2B', 3: '3B', 4: 'C', 5: 'OF1', 6: 'OF2', 7: 'OF3', 8: 'SP1', 9: 'SP2', 10: 'SS'})
49
  try:
50
  df['Own'] = df['Own'].str.replace('%', '').astype(float)
51
  except:
 
26
  df['EntryCount'] = df['EntryCount'].fillna('1/1') # Default to 1/1 if no entry count
27
 
28
  # Split the lineup string by replacing position indicators with commas
 
 
29
  df['Lineup'] = df['Lineup'].str.replace(r'\b(' + '|'.join(pos_values) + r')\b', r'\1,', regex=True)
30
 
31
  # Split into individual columns and remove position indicators
 
37
  return None
38
 
39
  # Create columns for each player
40
+ for i in range(max_players):
41
+ # Get the player at this position, or empty string if not present
42
+ df[i] = df['Lineup'].str.split(',').str[i].fillna('').str.strip()
43
  # Remove position indicators from the end of each entry
44
  df[i] = df[i].str.replace(r'\s+(' + '|'.join(pos_values) + r')$', '', regex=True)
45
+ # Replace empty strings with a meaningful value if desired
46
+ # df[i] = df[i].replace('', 'Empty') # Uncomment if you want to use 'Empty' instead of empty strings
47
 
48
  if sport == 'MLB':
49
+ df = df.rename(columns={0: '1B', 1: '2B', 2: '3B', 3: 'C', 4: 'OF1', 5: 'OF2', 6: 'OF3', 7: 'SP1', 8: 'SP2', 9: 'SS'})
50
  try:
51
  df['Own'] = df['Own'].str.replace('%', '').astype(float)
52
  except: