Please help to understand 'loc' function

https://app.dataquest.io/m/381/exploring-data-with-pandas%3A-fundamentals/12/challenge-top-performers-by-country

My code:

boo_1 = f500["country"] == "USA"

industry_usa = f500[boo_1, "industry"]

Error:
'Series' objects are mutable, thus they cannot be hashed

Question: I searched around I figured out the objects “Series” here can be changed, hence “mutatable”, so they cannot be hashed (meaning compared or changed). I have no idea how this error happened, can someone explain to me please?

Note: I added ‘loc’ and it works. I hypothesized that because with ‘loc’ function, it refers to the string as an index, hence it works. Is my hypothesis correct?

1 Like

Hey there!

Since you’re trying to index on both dimensions, you’ll need to use .loc[] here — you can omit it if you’re using shorthand to select multiple columns, but since you’re looking to select specific rows via boolean filtering, you’ll need to use the correct syntax.

As far as understanding the error, this is one of the better put ways that I’ve seen floating around: https://www.quora.com/How-can-I-resolveTypeError-Series-objects-are-mutable-thus-they-cannot-be-hashed-error-in-Python

2 Likes

Thank you Dustin. I read the quora’s answer, super helpful!