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?

Answer

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