Boolean sentences

My Code:
jfk = taxi[taxi[:, 6] ==2]
jfk_count = jfk.shape[0]

laguardia = taxi[taxi[:, 6] ==3]
laguardia_count = laguardia.shape[0]

newark = taxi[taxi[:, 6] ==5]
newark_count = newark.shape[0]

print(‘JFK:’, jfk_count)
print(‘Laguardia:’, laguardia_count)
print(‘Newark:’, newark_count)

Why it said jfd is not defined if i put " taxi[taxi[:, 6] ==2] = jfk"?

Hi @ipngasi:

Please attach the mission link and format your code accordingly as per these guidelines so we know the particular mission you are referring to. Thanks!

the Link:
https://app.dataquest.io/m/290/boolean-indexing-with-numpy/9/challenge-which-is-the-most-popular-airport

jfk = taxi[taxi[:, 6] ==2]
jfk_count = jfk.shape[0]

laguardia = taxi[taxi[:, 6] ==3] 
laguardia_count = laguardia.shape[0]

newark = taxi[taxi[:, 6] ==5] 
newark_count = newark.shape[0]

print('JFK:', jfk_count)
print('Laguardia:', laguardia_count)
print('Newark:', newark_count)

Why it said jfd is not defined if i put " taxi[taxi[:, 6] ==2] = jfk"?

I follow the guidelines and is it clear enough?

Hey I just copy-pasted your code and I got a big green Nice Work!

By the way the code above is wrong. Try:

taxi[taxi[:, 6] ==2] == jfk

@WilfriedF I believe this code is unecessary in our case here since == is the equality operator vs = which is the assignment operator (see here). Hence, the above line of code will check to see if the expressions on the left and right of == are in fact equal and will simply return True or False which is not something we need to do here.

I’m going to go out on a limb here and guess that @ipngasi is asking why they get an error for taxi[taxi[:, 6] ==2] = jfk as opposed to jfk = taxi[taxi[:, 6] ==2]. If so, the reason for this is python syntax.

When assigning a value (like: 5, “Some text”, or taxi[taxi[:, 6] ==2]) to a variable (like : x, string, or jfk), python requires you to place the variable name to the left of = and the value you want to assign to it to its right. I always like to think of this as reading from right to left: "assign the value of right side to the variable left side.

In other words, taxi[taxi[:, 6] ==2] = jfk will take the value of jfk and will try to store it in taxi[taxi[:, 6] ==2]. This will cause an error if there is no value currently stored in jfk. If jfk did have a value assigned to it, this line of code would assign that value to every row that has a value of 2 in the 6th column of taxi. This is not what we want to do in this particular case. This could be helpful in other situations though. Think of this line of code like a “filtered assignment.”

That said, what we actually want to do here is to assign the value of taxi[taxi[:, 6] ==2] to the variable jfk and we do that using this syntax:

variable = value

Therefore the correct syntax is: jfk = taxi[taxi[:, 6] ==2]

Hope this makes things more clear and not less! :sunglasses:

Thanks a lot for both of you.

I understand now after you mentioned the value should assign to variable at the left.

:thinking:

You went so deeply in his mind @mathmike314 ! Think we need the opinion of @ipngasi because indeed it’s hard to know exactly what was the problem since his code is working perfectly.

@WilfriedF call it “teacher’s intuition” :sunglasses:

Also it was the key word if that tipped me off in the sentence “Why it said jfd is not defined if i put " taxi[taxi[:, 6] ==2] = jfk”?" – which implied that his question was “hypothetical” and not coming from the actual code provided. I did have to assume that jfd was a typo of jfk though…

In the end, it looks like that was the case!

1 Like

Right! The word “if” was the key here. I assumed the typo too but not the word “if”! Well done.

1 Like