0

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?

Latika Agarwal
  • 973
  • 1
  • 6
  • 11
Jasper
  • 2,131
  • 6
  • 29
  • 61
  • 1
    You may need `axis=1` in your `df.apply(` (not tested) – phi May 26 '18 at 13:58
  • That did the trick! Thanks for your response! I normally dont use 'apply' functions against the entire dataframe, so using the axis did not occur to me. – Jasper May 26 '18 at 14:00

1 Answers1

3

Here is a simpler way to achieve what you want

col_list = ['UK_midi', 'NL_midi', 'BE_midi', 'FR_midi', 'SP_midi', 'PT_midi']
df['COL_MIDI'] = (df[col_list].sum(axis=1) > 0) * 1
phi
  • 10,572
  • 3
  • 21
  • 30