There are 4 columns in total.
VALUE_DIFF is the calculated value. This is based on if column "SECOND" has a value greater than 0, then subtract column value from "SECOND" and "BASE_COLUMN" [ SECOND - BASE_COLUMN]. If column "SECOND" is 0 then subtract column value from "FRIST" and "BASE COLUMN" [FIRST - BASED_COLUMN]
Wondering how can Pandas execute with this condition?
CodePudding user response:
Use numpy.where
:
df['DIFF'] = np.where(df['SECOND'].gt(0), df['SECOND'], df['FIRST']) - df['BASE_COLUMN']
What is same like:
df['DIFF'] = np.where(df['SECOND'].gt(0),
df['SECOND']- df['BASE_COLUMN'],
df['FIRST']- df['BASE_COLUMN'])