290-8 Assignment Using Boolean Arrays Continued

My Code:

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

taxi_modified[taxi_modified[:,5]==(2 or 3 or 5),15] = 1
``````

What I expected to happen:

``````array([[  2016,      1,      1, ...,  69.99,      1,      1],
+        [  2016,      1,      1, ...,   54.3,      1,      1],
+        [  2016,      1,      1, ...,   37.8,      2,      1],
...,
``````

What actually happened:

``````ValueErrorTraceback (most recent call last)
<ipython-input-1-6c24b76f813d> in <module>()
5
6 oper = taxi_modified[:,5]
----> 7 taxi_modified[np.any(2<=taxi_modified[:,5]<=5),15] = 1

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
``````

Since the screen was on shortening the code, I tried to be a bit ambitious by combining three codes

``````taxi_modified[taxi_modified[:, 5] == 2, 15] = 1
taxi_modified[taxi_modified[:, 5] == 3, 15] = 1
taxi_modified[taxi_modified[:, 5] == 5, 15] = 1
``````

to one. However, the or operator does not seem to work properly within brackets. Or is there something that misunderstood when using the operator?

1 Like

You canâ€™t use Python logical arguments in numpy. He answered your question in this topic.

2 Likes

@kwonowk

In addition to what the @cem.ayhan has provided:

`taxi_modified[((taxi_modified[:, 5] == 2) | (taxi_modified[:, 5] == 3) | (taxi_modified[:, 5] == 5)), 15] = 1`

3 Likes

I guess you got why that did not work from @cem.ayhan reply. and more

Here we can also use `np.isin` to get same

``````taxi_modified[np.isin(taxi_modified[:, 5], [2,3,5]), 15] = 1
``````

Good to know. Thanks so much!

I should be more creative next time. Thanks @DishinGoyani!

No problem.

I have re-formatted your Original post to make more readable using markdown code (Surrounding code block by ````` three tilde signs).

Here is some guideline that you can use in future question It will help you to get faster and accurate reply.