Pandas 系列:布尔值的真值不明确

Pandas series: Truth Value of a Boolean is Ambiguous

所以我不确定为什么这会以一种方式起作用而不是另一种方式。

我有一个计算值并将其与静态列进行比较的列。

当我比较这两个具有函数 print(column1>column2) 时,我得到了一系列不错的 True/False 值。

所以它在那里工作,但是当我尝试将相同的不等式合并到 if/else 语句时,它会引发以下错误:

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

这是为什么?

代码如下:

def airportcheck(x1, y1):
    point1 = line(line1m, line1b, x1, y1)
    point2 = line(line2m, line2b, x1, y1)
    point3 = line(line3m, line3b, x1, y1)
    point4 = line(line4m, line4b, x1, y1)
    if point3>=y1:
        print(1)
    print(point3>y1)
    #if point1 > y1 and point3 > y1 and point4 < y1 and point2 < y1:
    #    return 1
    #else:
    #    return 0


df_data['Airport'] =(airportcheck(df_data['Pickup_longitude'], df_data['Pickup_latitude']))

不需要 apply 或 lambda 函数 - 只需使用 all() 来检查整个系列中的每一个的真实性。

if (point1 > y1).all() and \
   (point3 > y1).all() and \
   (point4 < y1).all() and \
   (point2 < y1).all():
    # ...