Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().

like image 781
ebertbm Avatar asked Feb 01 '26 12:02

ebertbm


1 Answers

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()
like image 71
mgilson Avatar answered Feb 03 '26 07:02

mgilson