Screen Link: https://app.dataquest.io/m/290/boolean-indexing-with-numpy/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?