application_vnd.jupyter.stderr_output_1.png
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

pd.set_option('display.max_columns', None)

train_df = pd.read_csv('/kaggle/input/football-match-probability-prediction/train.csv')
test_df = pd.read_csv('/kaggle/input/football-match-probability-prediction/test.csv')
sample_sub = pd.read_csv('/kaggle/input/football-match-probability-prediction/sample_submission.csv')

def number_of_previous_games(row, home):
    for i in range(1, 11):
        col = f'{home}_team_history_match_date_{i}'
        if pd.isna(row[col]):
            return i - 1
    return i
train_df['number_home_previous_games'] = train_df.apply(lambda row: number_of_previous_games(row, 'home'), axis=1)
train_df['number_away_previous_games'] = train_df.apply(lambda row: number_of_previous_games(row, 'away'), axis=1)
test_df['number_home_previous_games'] = test_df.apply(lambda row: number_of_previous_games(row, 'home'), axis=1)
test_df['number_away_previous_games'] = test_df.apply(lambda row: number_of_previous_games(row, 'away'), axis=1)

historical_columns_template = list(set(['_'.join(x.split('_')[:-1]) for x in train_df.columns.values.tolist() if x[-1].isnumeric()]))

is_cup_columns = [x for x in historical_columns_template if 'is_cup' in x]
print(is_cup_columns)
for col in is_cup_columns:
    for i in range(1, 11):
        train_df[col + f'_{i}'].fillna(0, inplace=True)
        test_df[col + f'_{i}'].fillna(0, inplace=True)