用于比较两个数据帧列的pyspark中的if else with and运算符

2024-09-28 03:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图compare two columns的一个数据帧,但它的结果是错误的。PFB代码:

if((df3.name==df3.KEY) and (df3.id==df3.seq_id)):
    print("hey")
else:
    print("hey1")

其中df3是数据帧。它抛出以下错误:

raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.  

我在SO blog中尝试了几种解决方案,并将操作符从and更改为&,但都没用

我尝试转换到pandas,然后执行操作,它在shell中运行良好,但在IDE中不起作用:

df4 = df3.toPandas()
if(df4['name'].equals(df4['KEY']) & df4['id'].equals(2)):
    print("hey")
else:
    print("hey1")

Tags: and数据keynameidforif错误

热门问题