Basj
Basj

Reputation: 46443

Test equality of a dataframe value with Pandas

When doing this:

import pandas as pd
df = pd.DataFrame({'A': ['1.0', '1.1', '1.2', '1.3'], 'B': ['a', 'b', 'c', 'd']})

row1 = df[df['A'] == '1.1']

if row1['B'] == 'b':  
    print('hello')

we get:

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

Why is it ambiguous?

It is solved indeed if I use any() or all() but here I'm reluctant about adding any or all without fully understanding why it is necessary. Usually any or all is necessary when we have a list of boolean values, but here we have only one such boolean.

TL;DR: how to test the equality of one dataframe value with Pandas?

Upvotes: 0

Views: 83

Answers (1)

jezrael
jezrael

Reputation: 862601

I think you can check this:

pandas follows the NumPy convention of raising an error when you try to convert something to a bool. This happens in an if-statement or when using the boolean operations: and, or, and not. It is not clear what the result of the following code should be:

if pd.Series([False, True, False]):
    pass

Should it be True because it’s not zero-length, or False because there are False values? It is unclear, so instead, pandas raises a ValueError:

if pd.Series([False, True, False]):
    print("I was true")

Traceback

ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().

You need to explicitly choose what you want to do with the DataFrame, e.g. use any(), all() or empty(). Alternatively, you might want to compare if the pandas object is None:

if pd.Series([False, True, False]) is not None:
    print("I was not None")

I was not None

Below is how to check if any of the values are True:

 if pd.Series([False, True, False]).any():
    print("I am any")
I am any

To evaluate single-element pandas objects in a boolean context, use the method bool():

pd.Series([True]).bool()
True

pd.Series([False]).bool()
False

pd.DataFrame([[True]]).bool()
True

pd.DataFrame([[False]]).bool()
False

Upvotes: 3

Related Questions