Boolean masks on 2-D array - row by row or col by col

Screen Link: https://app.dataquest.io/m/509/datasets-and-boolean-indexing/9/boolean-masks-in-higher-dimensions

To me it looks like when we apply boolean masks on 2-D array, the elements are taken in a col by col order, as opposed to the row by row order (see the figure above).

If it was operating row by row, the elements 2 and 4 would appear first in the resulting array.

Correct me if I’m wrong.

1 Like


1.The size of bool array and size of the array(axis wise) you want to filter , they have to be the same.
2.It’s not python who will decide along which axis the code will work, It’s you who will decide that your filter will work along which axis(axis=0 / 1st axis(vertically) or axis=1 / 2nd axis(horizontally)).
3. arr[bool_1] = > Here you are asking to work it along 1st axis , See you keep the bool_1 variable where we keep the row index,your column index is not defined here.
4.arr[:, bool_1] => Here you are asking to work it along 2nd axis , See you keep the bool_1 variable where we keep the column index,you selected all row indexes before it by “:” .
5.taxi[trip_mph_bool,5:9] => Here you are asking to work it along 1st axis , See you keep the trip_mph_bool variable where we keep the row index,you selected multiple specific columns indexes(5,6,7 and 8 since 9 is excluded) after it by “5:9”.


I hope I cleared the confusion, Please let me know.

1 Like

I don’t think you’ve read my question. I am not asking how boolean indexing works. I am asking about the order in which elements are taken to create the resulting ndarray.

1 Like

My bad! yeah you are right, that mask is working row by row.

If it was operating row by row, the elements 2 and 4 would appear first in the resulting array. But that’s no happening.

Hi @ prateek Prateek !
You can see the 1st row has a empty space or index in front of 2, so, the 2nd row’s 1st True element 7 has got the index to be seated. And, see when 1st row’s 4 and second row’s 1 are encountering each other for the place of index 2, 4 is getting the priority and got the place and 1 has to lose and has got the next place to 4.
Why is it happening?
— Because of “row by row” order.

1 Like

Thanks that makes sense.

1 Like

Thank you prateek Prateek :heart: