Ajay Chinni
Ajay Chinni

Reputation: 840

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

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?

Upvotes: 5

Views: 25550

Answers (4)

qagourab
qagourab

Reputation: 11

In my case , I was trying to push df directly into MongoDB as below-

db.collection.insertMany(df) 

but I need to send as below

db.collection.insertMany(df.to_dict('records'))

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71580

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

Upvotes: 2

jezrael
jezrael

Reputation: 862761

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]

Upvotes: 2

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

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")

Upvotes: 4

Related Questions