1

I have a dataframe that includes data from different age groups - and I want to remove all children (or keep all adults). So, I want to drop all rows which satisfy the condition:

df.age_category != "newborn" & != "child" & != "schoolage"

I know this question has been asked a couple of times and I have tried so many of the approaches on here (e.g. this, this, this, or this).

Each time I get the following error:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

This happens even when I only use one condition, so it doesn't seem to be a problem with the or / and statements. It also doesn't matter whether I assign the condition to a variable and then use that or if I use a loop with if statements. And it happens whether I want to get rid of children or keep the adults in the dataset, so it's not a problem with the df.drop() function, either.

halfer
  • 19,824
  • 17
  • 99
  • 186
elm1199
  • 11
  • 2
  • Use, [`isin`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.isin.html) ```df[~df.age_category.isin(["newborn", "child", "schoolage"])]``` – sushanth Jul 11 '20 at 12:43
  • 1
    That finally worked, thank you so much!!! – elm1199 Jul 11 '20 at 12:46

1 Answers1

0

Change

df.age_category != "newborn" & != "child" & != "schoolage"

to

all(df.age_category != "newborn",df.age_category != "child",df.age_category != "schoolage")
Red
  • 26,798
  • 7
  • 36
  • 58