Search code examples
pythonh2o

How to use custom function in apply method with H2O frame


I am trying to use the following code to apply function to each of the rows in a particular column of H2O frame, but getting error.

df["decision"] = df["resp_cd"].apply(lambda x:1 if x in ["00", 01", "11] else 0, axis=1)

enter image description here

Is there some other way to use user defined functions in H2O?

How should we use apply function in H2O? I expect it to be similar to apply function in Pandas but that doesn't seem to be true.


Solution

  • Totally reasonable question. In your case you should actually use the .ifelse() method instead of the apply method, H2O’s apply method is limited to a set of implemented math functions which can be used in a limited way in a lambda style function as well (note your error is just saying you are trying to use apply with an unimplimented method):

    The following may solve your problem but you may need to play around with it:

    (df[‘resp_cd’].isin([‘00’,’10’,’11’])).ifelse(1,0)

    (Statement you want to test).ifelse(return if true,return if false).

    You may need to change your column type to string or enum type with df[‘resp_cd’].ascharacter() or df[‘resp_cd’].asfactor() to get the code example above to work for you, but now you have a sense of how to apply a Boolean if else function to an H2OFrame.