Skip to main content
 首页 » 编程设计

python之ValueError:Series 的真值不明确。比较数据框列时使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

2024年09月07日8qq号

df['svc_port'] = np.where( 
     min(df['sPort'],df['dPort']) <= 1024,  
     min(df['sPort'],df['dPort']),  
     df['dPort'] 
) 

在上面的代码中,min(df['sPort'],df['dPort']) <= 1024 - 同样的事情在 Compare two columns using pandas 中给出.我没有使用任何逻辑运算符。只是检查一个条件并替换它的值。

为什么我仍然收到此错误?

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

请您参考如下方法:

您正在寻找元素方面的 min。内置的 min 函数仅适用于单个可迭代对象,不能同时用于多个可迭代对象。

您正在寻找的是沿着这些思路的东西,使用 np.minimum。

v = np.minimum(df['sPort'], df['dPort']) 
df['svc_port'] = np.where(v <= 1024, v, df['dPort'])