Home > Net >  pandas and if else produces unexpected results
pandas and if else produces unexpected results

Time:04-28

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?!

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)

CodePudding user response:

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)

CodePudding user response:

You shouldn't be using bitwise &. Instead, you should use and because its a list comprehension. Code:

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

Another method (discussed in comments by @Sembei):

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

Output:

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