.loc More Detail - Explo... w/ Pandas: Fundamentals

Hi again. I completed this section but I wanted a bit more clarity/detail around a specific topic: the .loc method.

  • Outside of memorization, is there a way to understand when I am to use it? I noticed to encapsulate my boolean I need to use it. But would you be able to please explain what it means?

  • Perhaps translate one of the lines below to pseudocode/plain English, and explain what is going on requiring the syntax to be as such, please?

  • Also, can you speak on any other scenarios where it would be different than below.

I’d like as much detail as possible as I looked around but could not find what i was looking for. Happy to engage in a discussion also!

:slight_smile:

top_3_countries = f500["country"].value_counts().head(3)

industry_usa = f500.loc[f500['country']=='USA', 'industry'].value_counts().head(2)
sector_china = f500.loc[f500['country']=='China', 'sector'].value_counts().head(3)
mean_employees_japan = float(f500.loc[f500['country']=='Japan', 'employees'].mean())

Hey.

I suggest you review this from screen 5 onward.

As is explained in the mission, DataFrame.loc allow us to select portions of a dataframe. We can pass two “kinds” of input to it, one concerns the location of the rows, the other the location of the columns: df.loc[row_labels, column_labels].

We use row_labels to select rows, and column_labels to select columns. And we can use these with multiple kinds of inputs. Let’s read from the documentation.

I won’t dive into ins and outs here, I think the mission does a good job at that. If you can be more specific in your question, we can try to help.

The object f500["country"] is the same as f500.loc[:, 'country'].

The object f500.loc[f500['country']=='USA', 'industry'] is the same as f500[f500['country']=='USA']['industry']. The remaining ones are similar to this.

1 Like