How to compare all columns with one column in pandas?
You can use df.apply
:
In [670]: df.iloc[:, :-1]\
.apply(lambda x: np.where(x > df.THRESHOLD, 1, -1), axis=0)\
.add_suffix('_CALC')
Out[670]:
A_CALC B_CALC
Date
2011-01-01 -1 -1
2012-01-01 -1 -1
2013-01-01 1 1
2014-01-01 1 1
2015-01-01 1 1
If THRESHOLD
is not your last column, you'd be better off using
df[df.columns.difference(['THRESHOLD'])].apply(lambda x: np.where(x > df.THRESHOLD, 1, -1), axis=0).add_suffix('_CALC')
Or maybe you can try this , by using subtract
, should be faster than apply
(df.drop(['THRESHOLD'],axis=1).subtract(df.THRESHOLD,axis=0)>0)\
.astype(int).replace({0:-1}).add_suffix('_CALC')