我想根据几个非空列过滤spark Dataframe sdf
。
假设我有:
labels = ["A", "B, C"]
这是可行的:
sdf.where(sf.col(labels[0]).isNotNull() | sf.col(labels[1]).isNotNull() | sf.col(labels[2]).isNotNull())
但是,如果列表很长,我想做一些类似于列表解析的事情:
sdf.where(any([sf.col(l).isNotNull() for l in labels]))
(this不起作用,{ValueError}无法将列转换为布尔值:请使用'&'表示'和','|“”表示“或”,“~”表示“not”。)
我怎样才能做到这一点?
1条答案
按热度按时间dly7yett1#
您可以使用
functools
中的reduce
来迭代列列表并应用您的逻辑。在您的示例中,看起来像是要获取其中任何列都具有非空值的所有行(因此应该过滤掉所有空值行)。
如您所见,所有
null
值的行都被正确地过滤掉了,这是通过对isNotNull()
条件进行OR运算来完成的。