In a loop, I have built a string to be used as a column restriction in a panda dataframe :
conditions = pd.DataFrame()
for index, fold in df.iterrows():
first_row = True
for elem in fold['include_months']:
if first_row:
condition = f"""('MonthNumber_"""+str(elem)+"' == 1)"
first_row = False
else:
condition += f""" | ('MonthNumber_"""+str(elem)+"' == 1)"
But I get an error with condition applied to panda column:
X_train = X_train[condition]
KeyError: "(X_train['MonthNumber_1'] == 1)| (X_train['MonthNumber_2'] == 1)"
How to fix it please?