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

pandas and if else produces unexpected results

I am sorry this is a simple one but why does the below code produce:

   val  trans
0    1      1
1    2      1
2    3      1
3    4      1
4    5      1
5    6      1
6    7      1

and not:

   val  trans
0    1      0
1    2      0
2    3      0
3    4      1
4    5      1
5    6      0
6    7      0

trans should be 1 for all vals greater equal 4 and less than 6?!

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

import pandas as pd

data = {'val':[1, 2, 3, 4, 5, 6, 7]}
 
df = pd.DataFrame(data)
print(df.info())

df['trans'] = [1 if x >= 4 & x < 6 else 0 for x in df['val']]

print(df)

>Solution :

Because working with scalars in list comprehension use and instead bitwise AND working with arrays:

df['trans'] = [1 if x >= 4 and x < 6 else 0 for x in df['val']]

print(df)
   val  trans
0    1      0
1    2      0
2    3      0
3    4      1
4    5      1
5    6      0
6    7      0

Vectorized solutions:

df['trans'] = np.where((df.val >= 4 ) & (df.val < 6) , 1, 0)
df['trans'] = ((df.val >= 4 ) & (df.val < 6)).astype(int)

Or:

df['trans'] = np.where(df.val.between(4, 5), 1, 0)
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