Screen Link:
My Code:
industry_usa = f500.loc[[f500['country'] == 'USA'],["industry"]].value_counts().head(2)
Please can someone explain why this doesn’t work? The course hasn’t explained the solution provided at all.
Screen Link:
My Code:
industry_usa = f500.loc[[f500['country'] == 'USA'],["industry"]].value_counts().head(2)
Please can someone explain why this doesn’t work? The course hasn’t explained the solution provided at all.
You need to be a bit more careful with how and where to use the brackets.
What you are trying to do is close to this particular example in the documentation
Conditional that returns a boolean Series with column labels specified
df.loc[df['shield'] > 6, ['max_speed']]
When you want to select some rows based on a boolean condition, there is no need for a bracket around that condition for extracting those rows.
So, instead of -
f500.loc[[f500['country'] == 'USA'],["industry"]]
you would have -
f500.loc[f500['country'] == 'USA',["industry"]]
That’s the primary source of your error.
When you fix the above, you will get another error because of value_counts()
. The reason being, that when using .loc()
if you enclose the column(s) in double brackets, it returns a dataframe. And a Pandas dataframe does not have a value_counts() method.
You need the loc() to return a Series instead. And for that, you don’t need a bracket around "industry"
.
So, your original code -
industry_usa = f500.loc[[f500['country'] == 'USA'],["industry"]].value_counts().head(2)
becomes
industry_usa = f500.loc[f500['country'] == 'USA',"industry"].value_counts().head(2)