Dataframe.isin()给出此错误:DataFrame的真值不明确

ebe*_*tbm 1 python python-3.x pandas

您可以解决此错误吗:df.isin函数在做什么错?

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"
Run Code Online (Sandbox Code Playgroud)

这给了我这个错误:

ValueError:DataFrame的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

mgi*_*son 6

df[df['date'].isin(existing_dates)]返回一个数据框。与正常序列不同,DataFrames继承了它们的真实性,numpy.ndarray即不允许您对其进行真实性检查(除非它的长度为1-这很奇怪)。

解决方案取决于您要从该表达式中获取什么...例如,如果要检查是否至少有一个元素:

len(df[df['date'].isin(existing_dates)])
Run Code Online (Sandbox Code Playgroud)

或者您是否要检查所有元素是否“真实”:

df[df['date'].isin(existing_dates)].all()
Run Code Online (Sandbox Code Playgroud)