Question about Boolean Indexing with NumPy

Hi all! I have 2 questions about below boolean code. I try to understand the logic here…

-The first line of code is to take out the pickup month column.
-The second line is to compare the value in the pickup column to 1. I try to print the january_bool here, the output is [ True True True … False False False]
-The 3rd line means…? for my understanding, we try to assign some values of pickup_month column to the variable January. For example,in the last few lessons,we did B=A[3], the code means that we store the values at the A row index 3 position to the B. It has to be a specific number inside to identify the position. but here, we put [january_pool] which is [ True True True … False False False], so how can it get the output?
-The 4th line, i understand the .shape function, but what [0] means?

Thank you!!!

pickup_month = taxi[:,1]
january_bool = pickup_month == 1
january = pickup_month[january_bool]
january_rides = january.shape[0]

Hello @candiceliu93,

Kindly ensure to format your code properly, read up on this post for how to do so.

For your first question, In boolean indexing, we index a Series or Dataframe with a Series containing boolean values such that it only returns the values in the Dataframe which matches with the True value in the boolean series.
Hence, january_bool is the series used to index the pickup_month series such that it returns to the january variable, only the values that have a True value equivalent in the pickup_month series.
Read more about this here.

For the second question, read this post.

I hope this helps.

1 Like