使用pyspark时如何在条件中使用for循环?

阿莫尔

我正在尝试检查何时以及是否满足条件的多个列值0我们有spark数据框,其列从1到11,需要检查它们的值。目前我的代码如下:

df3 =df3.withColumn('Status', when((col("1") ==0)|(col("2") ==0)|(col("3") ==0)| (col("4") ==0) |(col("5") ==0)|(col("6") ==0)|(col("7") ==0)| (col("8") ==0)|(col("9") ==0)|(col("10") ==0)| (col("11") ==0) ,'Incomplete').otherwise('Complete'))

我如何仅通过使用for循环而不是那么多or条件来实现此目的

定量统计

您可以使用下面的代码来收集您的条件,并将它们加入单个字符串中,然后调用eval

cond ='|'.join('(col("'+str(_)+'")==0)' for _ in range(1, 12))

cond = '('+cond+')'

print(cond)

#((col("1")==0)|(col("2")==0)|(col("3")==0)|(col("4")==0)|(col("5")==0)|(col("6")==0)|(col("7")==0)|(col("8")==0)|(col("9")==0)|(col("10")==0)|(col("11")==0))

df3 = df3.withColumn('Status', when(eval(cond),'Incomplete').otherwise('Complete'))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章