-2

I have the following df:

A     B    C
100   2    3
-100  2    3

I want a new column that multiplies either column B or C by the value of column A:

if A<0:
D = A*B

if A>=0:
D = A*C

So in the end I get:

A     B    C   D
100   2    3   200
-100  2    3   -300
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Kosta S.
  • 339
  • 1
  • 2
  • 11

1 Answers1

0

You could run a lambda function based on what you are looking like this:

df["D"] = df.apply(lambda row: row["A"] * row["B"] if row["A"] < 0 else row["A"] * row["C"], axis=1)
Aidan Donnelly
  • 369
  • 5
  • 19