Going fast! #DataquestChallenge Premium Annual Offer:
500 get 50% & the next 1000 get 40% off.
GET OFFER CODE

Selecting Rows and columns for Numpy Boolean Indexing

Hi, I was wondering if you’ll could assist. In the example section for Question 6, Boolean Indexing, the following was presented:
With a 2D ndarray, just like with a 1D ndarray, we can assign one specific index location:

ones = np.array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
ones[1,2] = 99
print(ones)
[[ 1, 1, 1, 1, 1],
[ 1, 1, 99, 1, 1],
[ 1, 1, 1, 1, 1]]
this implies to insert 99 in the 2nd row, 3rd column, we should use: ones[1,2].

However, in the problem 1, when we were asked to change the column index 5, row index 1066, the Dataquest answer was:
taxi_modified[1066,5] = 1

My confusion: why didn’t we use taxi_modified[1065,4] = 1? This would make it consistent with the example.

Hi @johnssharma13 and welcome to the community!

I can definitely see how this can be confusing and I’m guessing this won’t be the last time that indexing causes a bit of a headache! In this situation, the confusion is coming from wording alone. We are asked to change column index 5, row index 1066. If we were to use taxi_modified[1065, 4] then we would be changing column index 4 and row index 1065. Now, it would be a different story if we were asked to modify the element in the 1066th row and 5th column…

Although it didn’t matter so much for this question, please provide a link to the mission in case more context is needed to help you with your question. Take a look at this post for some great suggestions when posting in the community.

Hi Mike,
Thanks for getting back to me. Yes, it seems to be more a matter of wording, it seems. But, it can be a bit confusing.

I have attached the link below.
https://app.dataquest.io/m/290/boolean-indexing-with-numpy/6/assigning-values-in-ndarrays

Thanks and have a nice day.

Kind Regards,

John

Thanks Mike.I clicked on the solution box.I wasn’t aware of it before.However, am getting more used to it.

Have a nice day.

John

Glad I was able to help you out and I hope you had a great weekend.

Happy coding and see you around the community!