2

I have a table with data for multiple continents. Now I want to delete every row where the continent isn't Europe or Africa. With:

df1 = df1.loc[(df1["region"] == "Europe")]

I get every row with "Europe" but I miss the "Africa" ones. Is there a way to include an "or"-operator? It doesn't work for me.

0

3 Answers 3

3

The question says drop, which needs to be adapted as follows if you want to drop the rows containing the mentioned value.

df1[(df1.region == "Europe") & (df1.region == "Africa")]

or you can also use the query command to achieve the same at the faster rate

df1.query("region == ['Europe', 'Africa']")
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure that query is faster? It is indeed shorter to type, but I am not sure for execution speed...
3

Also option df1[df1["region"].isin(["Europe", "Africa"])]

Comments

2

you can use

df1 = df1.loc[(df1["region"].isin(["Europe", "Africa"]))]

2 Comments

Apparently this is an invalid syntax. Python doesn't like the ".in".
ive made an edit it should be .isin()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.