Boolean Indexing with NumPy, Screen 8

Screen Link:

My Code:

for row in taxi_modified:
    row[5] = pickup_location_code
    row[15] = zeros_column
    if pickup_location_code == 2|3|5:
        zeros_column = 1

What I expected to happen:

What actually happened:

Traceback (most recent call last):
  File "/tmp/", line 86, in run_code_string
    exec(code, variables)
  File "<string>", line 10, in <module>
NameError: name 'pickup_location_code' is not defined

Why am I getting this error when I have assigned row[5] to pickup_location_code

The OR operator should only be used with Boolean datatypes.

When you write:

if pickup_location_code == 2|3|5

It’s causing problems because 2, 3, and 5 are neither True nor False. What you probably want is something like this:

if (pickup_location_code == 2)|(pickup_location_code == 3)|(pickup_location_code == 5)

That’s not the only way to do it but since each of these will evaluate to True or False, the OR operators will work.

There’s one more issue. Earlier, you wrote:

row[5] = pickup_location_code

When you did this, you set pickup_location_code equal to whatever is stored in row[5]. Notice that this is not the same as setting it equal to the number 5.

What you want to check in your if statement is whether or not pickup_location_code is equal to row[5], not whether or not it’s equal to 5. It might look something like this:

if  pickup_location_code == row[5]

Hope this helps!!

1 Like

Also, one hint: try to use Boolean indexing to solve your problem. I didn’t have to use a for loop at all to solve this one.

You can do something like this:

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

To do what you were trying to do.