I have simple dataframe:
import pandas as pd
frame = pd.DataFrame(np.random.randn(4, 3), columns=list(\'abc\'))
Thus for example:
use a vectorized approach
frame['d'] = frame.b + (frame.c > 0) * (frame.c - frame.b)
This is derived from the sum of
(frame.c > 0) * frame.c # frame.c if positive
Plus
(frame.c <= 0) * frame.b # frame.b if c is not positive
However
(frame.c <=0 )
is equivalent to
(1 - frame.c > 0)
and when combined you get
frame['d'] = frame.b + (frame.c > 0) * (frame.c - frame.b)