Каждый раз, когда мой код перебирает новый FRSW_RaceId, он присваивает Cover1200 и Cover1000 значение 0 для первой строки нового FRSW_RaceId.
См. вывод df в форме CSV здесь - 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
Подробнее здесь: https://stackoverflow.com/questions/793 ... t-meant-to
Не могу понять, почему код присваивает 0, если это не предназначено. ⇐ Python
Программы на Python
1736570100
Anonymous
Каждый раз, когда мой код перебирает новый FRSW_RaceId, он присваивает Cover1200 и Cover1000 значение 0 для первой строки нового FRSW_RaceId.
См. вывод df в форме CSV здесь - 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
Подробнее здесь: [url]https://stackoverflow.com/questions/79347528/cant-figure-out-why-code-assign-a-0-when-it-is-not-meant-to[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия