0

I'm running a query in a dataframe to retrieve a column value whenever a condition is met. For instance, I want to get col1 whenever col2=0.

result = df['col1'].loc[df['col2']==0]

Next, I want to check that

  1. The results are not empty, i.e. there is at least one instance where col2=0
  2. The returned value of col1 is not NaN

So I write:

if not result.empty and not result.isnull():
  pass

But this raises:

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

What am I actually supposed to do?

mrgou
  • 1,576
  • 2
  • 21
  • 45
  • All non-NaN or just one non-NaN ? – azro Jul 14 '20 at 21:07
  • `.isnull()` returns a Boolean array of the entire dataframe `True` or `False`. Will it be `None` if `all` or if `any`? – Trenton McKinney Jul 14 '20 at 21:08
  • 1
    Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Trenton McKinney Jul 14 '20 at 21:09
  • @azro, let's assume that we want all non-NaN. In practice, for my real-life case, the query will always return one row, as `col2` contains only unique values. @Trenton, I'm sure that's it, but I'm struggling to apply it to my case. – mrgou Jul 14 '20 at 21:21
  • 1
    It seems like only `if not result.empty:` is required. Otherwise `if not result.empty and not all(result.isnull()):` – Trenton McKinney Jul 14 '20 at 21:49

0 Answers0