mardi 27 septembre 2016

Python - In-line boolean evaluation without IF statements

I am trying to assess the value of a column of a dataframe to determine the value of another column. I did this by using an IF statement and .apply() function successfully. I.e. If Col x < 0.3 return y elif Col x > 0.6 return z

Etc. The problem is this takes quite a while to run with a lot of data. Instead I am trying to use the following logic to determine the new column value:

(x<0.3)*y + (x>0.6)*z

So Python evaluates TRUE/FALSE and applies the correct value. This seems to work much faster, the only thing is Python says: "UserWarning: evaluating in Python space because the '*' operator is not supported by numexpr for the bool dtype, use '&' instead unsupported[op_str]))"

Is this a problem? Should I be using "&"? I feel using "&" would be incorrect when multiplying.

Thank you!

Aucun commentaire:

Enregistrer un commentaire