would like some help with the following problem. I currently have a panda dataframe with 3 columns - test1, test2, test3
What I hope to achieve is result in the result_column, where the logic will be:
1) If value in test1 AND test2 > 0, then return value of test3
2) Else If value test1 AND test2 < 0, then return NEGATIVE value of test3
3) Otherwise return 0
test1 test2 test3 result_column
0 0.5 0.1 1.25 1.25
1 0.2 -0.2 0.22 0
2 -0.3 -0.2 1.12 -1.12
3 0.4 -0.3 0.34 0
4 0.5 0 0.45 0
This is my first time posting a question on python and pandas. Apologies in advance if the formatting here is not optimum. Appreciate any help I can get!
I think need numpy.select
with conditions chained by &
(AND
) or select all tested columns by subset [[]]
, compare ant test by DataFrame.all
:
m1 = (df.test1 > 0) & (df.test2 > 0)
#alternative
#m1 = (df[['test1', 'test2']] > 0).all(axis=1)
m2 = (df.test1 < 0) & (df.test2 < 0)
#alternative
#m2 = (df[['test1', 'test2']] < 0).all(axis=1)
df['result_column'] = np.select([m1,m2], [df.test3, -df.test3], default=0)
print (df)
test1 test2 test3 result_column
0 0.5 0.1 1.25 1.25
1 0.2 -0.2 0.22 0.00
2 -0.3 -0.2 1.12 -1.12
3 0.4 -0.3 0.34 0.00
4 0.5 0.0 0.45 0.00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With