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.

Patrick
  • 1,189
  • 5
  • 11
  • 19
Jeankrikri
  • 33
  • 5

3 Answers3

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']")
Roxy
  • 1,015
  • 7
  • 20
3

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

Gedas Miksenas
  • 959
  • 7
  • 15
2

you can use

df1 = df1.loc[(df1["region"].isin(["Europe", "Africa"]))]
Patrick
  • 1,189
  • 5
  • 11
  • 19