James McCool commited on
Commit
42712b2
·
1 Parent(s): c375de1

Add position mapping and eligibility checks in load_contest_file function

Browse files

- Implemented a position mapping dictionary to associate players with their respective positions.
- Added logic to create empty columns for MLB positions and fill them based on player eligibility.
- Enhanced lineup processing to ensure accurate assignment of players to required positions, improving data integrity and user experience.
- These changes contribute to ongoing efforts to refine player data handling within the application.

Files changed (1) hide show
  1. global_func/load_contest_file.py +48 -0
global_func/load_contest_file.py CHANGED
@@ -57,6 +57,54 @@ def load_contest_file(upload, sport):
57
  salary_df = df[['Player', 'Salary']]
58
  team_df = df[['Player', 'Team']]
59
  pos_df = df[['Player', 'Pos']]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS', 'Salary', 'Team'])
61
  cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]
62
  entry_list = list(set(df['BaseName']))
 
57
  salary_df = df[['Player', 'Salary']]
58
  team_df = df[['Player', 'Team']]
59
  pos_df = df[['Player', 'Pos']]
60
+
61
+ # Create position mapping dictionary
62
+ pos_dict = dict(zip(pos_df['Player'], pos_df['Pos']))
63
+
64
+ # Create empty columns for each position
65
+ if sport == 'MLB':
66
+ position_columns = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']
67
+ for col in position_columns:
68
+ df[col] = None
69
+
70
+ # Function to check if player is eligible for position
71
+ def is_eligible_for_position(player, target_pos):
72
+ if player not in pos_dict:
73
+ return False
74
+ player_positions = pos_dict[player].split('/')
75
+ # Handle special cases
76
+ if target_pos.startswith('SP') and 'P' in player_positions:
77
+ return True
78
+ if target_pos.startswith('OF') and 'OF' in player_positions:
79
+ return True
80
+ return target_pos in player_positions
81
+
82
+ # Process each lineup
83
+ for idx, row in df.iterrows():
84
+ # Get all players in the lineup
85
+ players = row['Lineup'].split(',')
86
+ players = [p.strip() for p in players if p.strip()]
87
+
88
+ # First pass: fill required positions (SP1, SP2, C, 1B, 2B, 3B, SS)
89
+ required_positions = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS']
90
+ for pos in required_positions:
91
+ for player in players:
92
+ if is_eligible_for_position(player, pos):
93
+ df.at[idx, pos] = player
94
+ players.remove(player)
95
+ break
96
+
97
+ # Second pass: fill OF positions with remaining players
98
+ of_positions = ['OF1', 'OF2', 'OF3']
99
+ for pos in of_positions:
100
+ for player in players:
101
+ if is_eligible_for_position(player, 'OF'):
102
+ df.at[idx, pos] = player
103
+ players.remove(player)
104
+ break
105
+ if not players: # If no more players, fill with -1
106
+ df.at[idx, pos] = '-1'
107
+
108
  cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS', 'Salary', 'Team'])
109
  cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]
110
  entry_list = list(set(df['BaseName']))