I have the following dataset:
target | date | US_midi | UK_midi | NL_midi | BE_midi | FR_midi | SP_midi | PT_midi | SU_midi | COL_MIDI
23 1970 1 0 0 0 0 0 0 0 0
34 1970 0 0 1 0 0 0 0 0 0
34 1970 0 1 1 0 0 0 0 0 0
Currently the COL_MIDI
value is set to 0, but I want to change it to one if any of the *_MIDI
columns, except the US_midi
and SU_midi
variables are set to 1
So in the example the last 2 rows would get a 1
for the COL_MIDI
variable
To this end I wrote the following function:
def col_checker(x):
col_list = ['UK_midi', 'NL_midi', 'BE_midi', 'FR_midi', 'SP_midi', 'PT_midi']
for nation in col_list:
if x[nation] != 0:
x['COL_MIDI'] = 1
Which I run with the following command:
df['COL_MIDI'] = df.apply(col_checker)
Yet everytime I encounter the following error (and the values stay at 0)
('UK_midi', 'occurred at index target')
I tried resetting the index (did not work) and I inspected both the dataframe, and saved the dataframe as a csv to then inspect it - but could not detect any anomalies. All columns have equal length and everything, so I am not sure how the index could have gotten messed up, nor why it is preventing my script from running.
Does anyone have an idea what I am doing wrong?