I am doing https://app.dataquest.io/m/292/exploring-data-with-pandas%3A-intermediate/9/using-boolean-operators-continued and am geting an error with the following code.
The label country is not in the index.
I have googled this and it appears it is caused by Dataframe[“index”][index"] of some kind but i cannot see an example of this in the code below
brazil_venezuela = f500.loc[(f500.loc[“country”] == “Brazil” or “Venezuela” )&(f500.loc[“country”] == “Venezuela”),:]
tech_outside_usa = f500.loc[(f500.loc[“country”] !=“USA”)&(f500.loc[“sector”]== “Technology”) ,:].head()
I understand the answer but want to grasp why my solution doesnt work.
I’m learning just like you! I think the issue is the way in which you try to index the column:
(Also, the exercise is asking for Brazil ’or’ Venezuela, so it should be | instead of &).
I think there is a better way to do this however which involves less code! You can index a column simply using the name of the column like so: dataframe_name[“column_name”].
This is what it’ll look like for the “brazil_venezuela” variable:
brazil_venezuela = f500[(f500["country"] == 'Venezuela') | (f500["country"] == "Brazil")]
I hope that answers your questions and helps!
@niaz is right!
You have to be careful about filtering columns with multiple conditions. Notice how @niaz’s answer used the bitwise operator
| instead of
or, and he also separated the different conditions using parenthesis.