# Boolean Condition for Numpy arrays - Mission 290-8

Guys I used two different ways of creating a boolean array to perform assignment operation
The first method runs and code is successful, the second method runs but the o/p prompt tells that output is incorrect. I want to know if my logic for the second one is incorrect or not?
Code for the both is given below, (Please let me know, Thank you!):

Code for first one (which is successful) :

``````# create a new column filled with `0`.
zeros = np.zeros([taxi.shape, 1])
taxi_modified = np.concatenate([taxi, zeros], axis=1)
print(taxi_modified)

pickup = taxi_modified[:,5]
condition = pickup == 2
taxi_modified[condition, 15] = 1
print(taxi_modified)

pickup = taxi_modified[:,5]
condition = pickup == 3
taxi_modified[condition, 15] = 1

pickup = taxi_modified[:,5]
condition = pickup == 5
taxi_modified[condition, 15] = 1
``````

Code for the second type:

``````# create a new column filled with `0`.
zeros = np.zeros([taxi.shape, 1])
taxi_modified = np.concatenate([taxi, zeros], axis=1)
print(taxi_modified)

pickup = taxi_modified[:,5]
condition = (pickup == (2 or 3 or 5))
taxi_modified[condition, 15] = 1
print(taxi_modified)
``````
2 Likes

Hello @dvbagul welcome the the DataQuest community.

You can’t use Python’s Logical conditions to subset a numpy array or even pandas Series/DataFrame, you need to use Numpy’s Logical operations

The above using Numpy Logic Operations will be as follows: ``````condition = np.logical_or(pickup==2, pickup==3, pickup==5)
``````

So your code for the second type should be as follows for your code to run successfully.

``````# create a new column filled with `0`.
zeros = np.zeros([taxi.shape, 1])
taxi_modified = np.concatenate([taxi, zeros], axis=1)
print(taxi_modified)

pickup = taxi_modified[:,5]
condition = np.logical_or(pickup==2, pickup==3, pickup==5)
taxi_modified[condition, 15] = 1
print(taxi_modified)
``````

I hope this has helps you.

8 Likes

Thank You, Victor for your thoroughly explaining the point. Now it works.  1 Like

Thank you @dvbagul for posting this. I stopped by to see if anyone else had this question. I figured that there had to be a simpler way to write the code, rather than repeating the same lines 3 times for each value that we wanted to check! I couldn’t figure it out, though. Thank you @info.victoromondi for sharing the solution!

1 Like