How to create a new column based on a condition in another column

In pandas, How can I create a new column B based on a column A in df, such that:

  • B=1 if A_(i+1)-A_(i) > 5 or A_(i) <= 10
  • B=0 if A_(i+1)-A_(i) <= 5

However, the first B_i value is always one

Example:

A B
5 1 (the first B_i)
12 1
14 0
22 1
20 0
33 1

>Solution :

Use diff with a comparison to your value and convertion from boolean to int using le:

N = 5
df['B'] = (~df['A'].diff().le(N)).astype(int)

NB. using a le(5) comparison with inversion enables to have 1 for the first value
output:

    A  B
0   5  1
1  12  1
2  14  0
3  22  1
4  20  0
5  33  1

updated answer, simply combine a second condition with OR (|):

df['B'] = (~df['A'].diff().le(5)|df['A'].lt(10)).astype(int)

output: same as above with the provided data

Leave a Reply