简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM