-1

I am trying to build a column that will be based off of another. The new column should reflect the values meeting certain criteria and put 0's where the values do not meet the criteria.

Example, a column called bank balance will have negative and positive values; the new column, overdraft, will have the negative values for the appropriate row and 0 where the balance is greater than 0.

 Bal  Ovr
 21   0
-34 -34
 45   0
-32 -32

The final result should look like that.

RoiMinuit
  • 404
  • 4
  • 17

2 Answers2

1

Assuming your dataframe is called df, you can use np.where and do:

import numpy as np
df['Ovr'] = np.where(df['Bal'] <0,'df['Bal'],0)

which will create a column called Ovr, with 0's when Bal is +ve, and the same as Bal when Bal is -ve.

sophocles
  • 13,593
  • 3
  • 14
  • 33
1
df["over"] = df.Bal.apply(lambda x: 0 if x>0 else x)

Additional method to enrich your coding skills. However, it isn't needed for such easy tasks.

ombk
  • 2,036
  • 1
  • 4
  • 16