Python ValueError:(';序列的真值不明确。请在索引0';处使用a.empty、a.bool()、a.item()、a.any()或a.all()

Python ValueError:(';序列的真值不明确。请在索引0';处使用a.empty、a.bool()、a.item()、a.any()或a.all(),python,pandas,Python,Pandas,我的功能如下 def fu1(): return ("func1") def fu2(): return ("func2") 我的代码在下面 def test_func(x): if (df['response'].str['detected'] == True & df['response_url1'].str['is_doc1'] == True): fu1() elif (df['response'].str['detected'

我的功能如下

def fu1():
    return ("func1")

def fu2():
    return ("func2")
我的代码在下面

def test_func(x):
    if (df['response'].str['detected'] == True & df['response_url1'].str['is_doc1'] == True):
        fu1()
    elif (df['response'].str['detected'] == True & df['response_url2'].str['is_doc1'] == True):
        fu2()
    else:
        return ("oops no response")
应用函数时

df.compare=df.apply(测试函数,轴=1)

得到下面的错误

ValueError:(“序列的真值不明确。请使用a.empty、a.bool()、a.item()、a.any()或a.all(),“发生在索引0处”)

df['response'].str['detected']

0    True
1    True
2    True
3    True
Name: response, dtype: bool
添加样本数据(头(2)

预料之外
df.比较
=
func2
oops无响应

试试这个

def test_func(x):
    if ((df['response'].str['detected'] == True) & (df['response_url'].str['is_doc1'] == True)):
        fu1()
    elif ((df['response'].str['detected'] == True) & (df['response_url2'].str['is_doc1'] == True)):
        fu2()
    else:
        return ("oops no response")
假设所有这些表达式的计算结果都是布尔级数,就像您的单个示例一样,请尝试

def test_func(x):
    if df['response'].str['detected'].all() and df['response_url'].str['is_doc1'].all():
        fu1()
    elif (df['response'].str['detected'].all() and df['response_url2'].str['is_doc1'].all():
        fu2()
    else:
        return ("oops no response")

或者使用
.equals()


请不要将此设置为重复,我已经检查了堆栈溢出链接salReady done any()和all(),您是否搜索了错误消息?错误消息会告诉您问题所在。df['response'].str['detected']包含四个布尔值,因此equals比较是不明确的。您希望序列中的所有条目都为真还是至少为一个?您是如何使用
any()
还是
all()
?相同的错误g,我希望串联比较,每列值对应于其他列您可以提供示例数据吗?添加了示例数据
def test_func(x):
    if df['response'].str['detected'].all() and df['response_url'].str['is_doc1'].all():
        fu1()
    elif (df['response'].str['detected'].all() and df['response_url2'].str['is_doc1'].all():
        fu2()
    else:
        return ("oops no response")
def test_func(x):
    q = df['response'].str['detected'] == df['response_url'].str['is_doc1']
    r = (df['response'].str['detected'] == df['response_url2'].str['is_doc1']

    if q.all():
        fu1()
    elif r.all():
        fu2()
    else:
        return ("oops no response")
def test_func(x):
    if df['response'].str['detected'].equals(df['response_url'].str['is_doc1']):
        fu1()
    elif df['response'].str['detected'].equals(df['response_url2'].str['is_doc1']):
        fu2()
    else:
        return ("oops no response")