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

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

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

您正在寻找 element-wise min。内置 min 函数仅适用于单个可迭代对象,不能同时用于多个迭代对象。

您正在寻找的是这些方面的内容,使用 np.minimum

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