0

I have a dataset and I want to drop all the rows that are now in my condition. This is my code:

covid = covid.drop(covid[covid.Country != "South Africa"].index)

I can drop all the rows where shouth africa is not the country, but i want to keep other countries apart from south africa, eg. South africa, UK, USA, India...

I have tryed:

covid = covid.drop(covid[covid.Country != "South Africa" and "UK"].index)
covid = covid.drop(covid[covid.Country != "South Africa" & "UK"].index)
covid = covid.drop(covid[covid.Country != "South Africa", "UK"].index)

but none of these work.

edshewa
  • 21
  • 1
  • 6
  • 3
    [This answer](https://stackoverflow.com/a/54358361/15497888) has an extensive explanation of the pandas boolean operators and how to use them. – Henry Ecker Oct 17 '21 at 16:47
  • When you use `and` or `or`, you have to specify the full condition. To be specific, in your example, replace with `covid.Country != "South Africa" and covid.Country !="UK"` – DeBARtha Oct 17 '21 at 16:48
  • 1
    However, You're likely (in this case) looking for something like [How to filter Pandas dataframe using 'in' and 'not in' like in SQL](https://stackoverflow.com/q/19960077/15497888) `covid = covid[covid.Country.isin(['South Africa', 'UK'])]` – Henry Ecker Oct 17 '21 at 16:50
  • Have you tried is `covid = covid[covid.Country.isin(["South Africa", "UK"])]` ? Since you are keeping only these, no need for dropping ;) Just `SELECT * FROM covid WHERE coutry in [A, B]` – Prayson W. Daniel Oct 17 '21 at 16:51

0 Answers0