Placement of Boolean Index

I had a question on “Exploring Data with Pandas:Fundamentals” Mission on Page 9.

URL: Learn data science with Python and R projects

I was playing around the code and I first tried this:
x=f500.loc[:,"country"][f500.loc[:,"industry"]=="Motor Vehicles and Parts"]

I then tried:
x=[f500.loc[:,"industry"]=="Motor Vehicles and Parts"]f500.loc[:,"country"]
and it gave an error.

All I did was reverse what I previously did. Does it matter where you put the boolean index?

Hi @mtl1212,

According to the syntax of using boolean masks on dataframes, these 2 parts of code f500.loc[:,"country"] and [f500.loc[:,"industry"]=="Motor Vehicles and Parts"] are not interchangeable, and if you try to change their places, you’ll receive a syntax error.

This piece of code:

f500.loc[:,"industry"]=="Motor Vehicles and Parts"

creates a boolean mask that compares whether the values in the industry column from the f500 dataframe are equal to "Motor Vehicles and Parts". If we want to apply this mask to the whole dataframe, we should use the following sintax:

boolean_mask = f500.loc[:,"industry"]=="Motor Vehicles and Parts"

However, we’re not interested in the whole dataframe here, we want only to identify countries with the companies belonging to the “Motor Vehicles and Parts”. So, we’ll filter only the country column and apply to it our boolean mask:

boolean_mask = f500.loc[:,"industry"]=="Motor Vehicles and Parts"

By the way, don’t forget to use the variable names motor_bool and motor_countries suggested in that mission screen (instead of boolean_mask and x, correspondingly), for the answer-checking purposes of the DQ platform.

1 Like

Hi @Elena_Kosourova,
Thanks for the reply. I did do it using the intermediate step. Sometimes I play around with the code or something will pique my interest from one mission and I go back to another mission. With coding, I like tinkering!

1 Like