ultraInstinct
ultraInstinct

Reputation: 4323

Dataframe.isin() giving this error: The truth value of a DataFrame is ambiguous

Can you help with this error: what am I doing wrong with the df.isin function?

cursor = con.cursor()
cursor.execute("""SELECT distinct date FROM raw_finmis_online_activation_temp""")
existing_dates = [x[0] for x in cursor.fetchall()]

if df[df['date'].isin(existing_dates)]:
    print "Yes it's in there"
else:
    print "N"

It's giving me this error:

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

Upvotes: 2

Views: 3256

Answers (1)

mgilson
mgilson

Reputation: 309929

df[df['date'].isin(existing_dates)] returns a dataframe. Unlike normal sequences, DataFrames inherit their truthyness from numpy.ndarray which is don't allow you to do a truth check on it (unless it has length 1 -- which is weird).

The solution depends on what you want out of that expression ... e.g. if you want to check if there is at least one element:

len(df[df['date'].isin(existing_dates)])

or if you want to check if all the elements are "truthy":

df[df['date'].isin(existing_dates)].all()

Upvotes: 6

Related Questions