Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading