ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() - when comparing string with dataframe

Viewed 8662

I have a list of data frames but in a few cases, the list can also contain a string.

df = pd.DataFrame({"df_column":["df_value"]})
a = ['skip',df]

if "skip" in a:
    print("yes")

The above gives output as yes because the list contains a string.

But in case if the list doesn't contain a string for eg

df = pd.DataFrame({"df_column":["df_value"]})
    a = [df,df]
    
    if "skip" in a:
        print("yes")

Now the above code gives an error ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). How can I handle this?

3 Answers

You need to avoid checking pandas all together see e.g. [df , 'skip'] would fail - it's just a matter of order.

For starters you can only filter strings in a:

if "skip" in filter(lambda x: isinstance(x, str), a):
    print("yes")

It's because of the handling of the pandas DataFrame magic methods.

So you could make them numpy arrays:

>>> 'skip' in [df.to_numpy(), df.to_numpy()]
False
>>> 'skip' in [df.to_numpy(), df.to_numpy(), 'skip']
True
>>> 

The reason is that the in functions checks equality for each element in the list, but obviously 'skip' == df would throw an error.

So the way to fix would be the above, or the below:

if "skip" in map(str, a):
    print("yes")

ins implementation might be roughly:

for element in sequence:
    if element == target:
        return True
return False

Filter out DataFrames, so comparing working nice:

df = pd.DataFrame({"df_column":["df_value"]})
a = [df,df,'skip']

#filter out DataFrames
#a = [x for x in a if not isinstance(x, pd.DataFrame)]
#or filter strings
a = [x for x in a if isinstance(x, str)]
print (a)
['skip']

if "skip" in a:
    print("yes")

One idea is convert DataFrames to strings by DataFrame.to_string in list comprehension, but it is slowier:

a = [x.to_string() if isinstance(x, pd.DataFrame) else x for x in a]
Related