0

I'm trying to create some new columns in pandas, based on the values of existing columns, like in Excel. I'm not trying to find a value or match condition, so pd.where is not the solution. I wanna create a new column based on the variables (columns) I have for each row.

for ex. I would like to create a column 'D' with the condition : if pd['A'] > pd['C'] --> pd['D'] = 0, else pd['D'] = (pd['C']-pd['B'])*pd['A']

   B   A   C  D
0   1   2   3  4
1   4   20  6  0
2   7   8   9  40
3   10  11  12 22
4   13  24  15  0

but I'm getting the error

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

My code is:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(1,16).reshape(5,3),columns={'A','B','C'})

df['D'] = df['A'].apply(lambda x: 0. if (x > df['C']) else (df['C']-df['B'])*df['A'] )
JCV
  • 447
  • 1
  • 5
  • 15

0 Answers0