Every time my code iterates over a new ‘FRSW_RaceId’, it assigns ‘Cover1200’ and ‘Cover1000’ a 0 for the first row of the new ‘FRSW_RaceId’.
Please see output of df in CSV form here – https://jmp.sh/s/saaQCt08U6vBiizDywbE
# Define the distances to loop through distance_columns = [1200, 1000, 800, 600, 400, 200] # Iterate through each distance column for distance in distance_columns: pos_column = f'SH_POS{distance}' wides_column = f'W_Wides{distance}' cover_column = f'Cover{distance}' # Initialize the cover column with NaN df[cover_column] = np.nan # Group by 'FRSW_RaceId' to process each race separately for race_id, race_group in df.groupby('FRSW_RaceId'): # If all wides_column values for the race are empty, set cover_column to NaN if race_group[wides_column].isna().all(): df.loc[df['FRSW_RaceId'] == race_id, cover_column] = np.nan continue # Assign 0 to cover_column for horses in position 1 df.loc[(df['FRSW_RaceId'] == race_id) & (df[pos_column] == 1), cover_column] = 0 df.loc[ (df['FRSW_RaceId'] == race_id) & ((df[wides_column] == 0) | (df[wides_column] == 99) | (df[pos_column] == 0) | (df[pos_column] == 25)), cover_column ] = np.nan # Process horses with positions greater than 1 # Ensure we ignore NaN values in pos_column valid_positions = race_group[pos_column].dropna().unique() if len(valid_positions) == 0: continue # Skip if no valid positions max_position = int(valid_positions.max()) # Get the maximum valid position for current_position in range(2, max_position + 1): # Get the horse at the current position current_horse = race_group[race_group[pos_column] == current_position] # Skip if no horse found (defensive coding) if current_horse.empty: continue current_index = current_horse.index[0] current_wides = current_horse[wides_column].values[0] # Get all horses ahead of the current horse horses_ahead = race_group[race_group[pos_column] < current_position] # Check if any horse ahead has the same wides number has_cover = any(horses_ahead[wides_column] == current_wides) # Assign cover status df.loc[current_index, cover_column] = 1 if has_cover else 0
submitted by /u/onthepunt
[link] [comments]
r/learnpython Every time my code iterates over a new ‘FRSW_RaceId’, it assigns ‘Cover1200’ and ‘Cover1000’ a 0 for the first row of the new ‘FRSW_RaceId’. Please see output of df in CSV form here – https://jmp.sh/s/saaQCt08U6vBiizDywbE # Define the distances to loop through distance_columns = [1200, 1000, 800, 600, 400, 200] # Iterate through each distance column for distance in distance_columns: pos_column = f’SH_POS{distance}’ wides_column = f’W_Wides{distance}’ cover_column = f’Cover{distance}’ # Initialize the cover column with NaN df[cover_column] = np.nan # Group by ‘FRSW_RaceId’ to process each race separately for race_id, race_group in df.groupby(‘FRSW_RaceId’): # If all wides_column values for the race are empty, set cover_column to NaN if race_group[wides_column].isna().all(): df.loc[df[‘FRSW_RaceId’] == race_id, cover_column] = np.nan continue # Assign 0 to cover_column for horses in position 1 df.loc[(df[‘FRSW_RaceId’] == race_id) & (df[pos_column] == 1), cover_column] = 0 df.loc[ (df[‘FRSW_RaceId’] == race_id) & ((df[wides_column] == 0) | (df[wides_column] == 99) | (df[pos_column] == 0) | (df[pos_column] == 25)), cover_column ] = np.nan # Process horses with positions greater than 1 # Ensure we ignore NaN values in pos_column valid_positions = race_group[pos_column].dropna().unique() if len(valid_positions) == 0: continue # Skip if no valid positions max_position = int(valid_positions.max()) # Get the maximum valid position for current_position in range(2, max_position + 1): # Get the horse at the current position current_horse = race_group[race_group[pos_column] == current_position] # Skip if no horse found (defensive coding) if current_horse.empty: continue current_index = current_horse.index[0] current_wides = current_horse[wides_column].values[0] # Get all horses ahead of the current horse horses_ahead = race_group[race_group[pos_column] < current_position] # Check if any horse ahead has the same wides number has_cover = any(horses_ahead[wides_column] == current_wides) # Assign cover status df.loc[current_index, cover_column] = 1 if has_cover else 0 submitted by /u/onthepunt [link] [comments]
Every time my code iterates over a new ‘FRSW_RaceId’, it assigns ‘Cover1200’ and ‘Cover1000’ a 0 for the first row of the new ‘FRSW_RaceId’.
Please see output of df in CSV form here – https://jmp.sh/s/saaQCt08U6vBiizDywbE
# Define the distances to loop through distance_columns = [1200, 1000, 800, 600, 400, 200] # Iterate through each distance column for distance in distance_columns: pos_column = f'SH_POS{distance}' wides_column = f'W_Wides{distance}' cover_column = f'Cover{distance}' # Initialize the cover column with NaN df[cover_column] = np.nan # Group by 'FRSW_RaceId' to process each race separately for race_id, race_group in df.groupby('FRSW_RaceId'): # If all wides_column values for the race are empty, set cover_column to NaN if race_group[wides_column].isna().all(): df.loc[df['FRSW_RaceId'] == race_id, cover_column] = np.nan continue # Assign 0 to cover_column for horses in position 1 df.loc[(df['FRSW_RaceId'] == race_id) & (df[pos_column] == 1), cover_column] = 0 df.loc[ (df['FRSW_RaceId'] == race_id) & ((df[wides_column] == 0) | (df[wides_column] == 99) | (df[pos_column] == 0) | (df[pos_column] == 25)), cover_column ] = np.nan # Process horses with positions greater than 1 # Ensure we ignore NaN values in pos_column valid_positions = race_group[pos_column].dropna().unique() if len(valid_positions) == 0: continue # Skip if no valid positions max_position = int(valid_positions.max()) # Get the maximum valid position for current_position in range(2, max_position + 1): # Get the horse at the current position current_horse = race_group[race_group[pos_column] == current_position] # Skip if no horse found (defensive coding) if current_horse.empty: continue current_index = current_horse.index[0] current_wides = current_horse[wides_column].values[0] # Get all horses ahead of the current horse horses_ahead = race_group[race_group[pos_column] < current_position] # Check if any horse ahead has the same wides number has_cover = any(horses_ahead[wides_column] == current_wides) # Assign cover status df.loc[current_index, cover_column] = 1 if has_cover else 0
submitted by /u/onthepunt
[link] [comments]