Question about exercise : 381-12


industry_usa = f500[“industry”][f500[“country”] == “USA”].value_counts().head(2)

May anyone explain why f500[“industry”] stands independently? I guess it should be
f500[“industry”, f500[“country”] == “USA”].value_counts().head(2)]

Thank you

Hi @hongchi0502, this is how the code works:

First, f500["industry"] returns the industry column in the f500 dataframe as Series.
Next, [f500[“country”] == “USA”] is used to filter the industry Series to return only the industries in USA.
Lastly, value_counts().head(2) is applied on the results obtained in step 2.

[f500[“country”] == “USA”] is a FILTER and cannot be used as an index in subsetting a dataframe. Rather, it can be applied on an entire dataframe or series to return all rows that meet the filter condition. That is why f500["industry"] is first used to obtain the industry Series and then [f500[“country”] == “USA”] is applied as a filter. So your suggested code is not valid and will return an error.

Let’s know if this helps.


It is really helpful, I appreciated

Hi @hongchi0502, please mark the post as solved if the response addresses your problem. See this post How do I mark a post as solved?