[英]Using Spark DataFrame filter with a List of column names
I have to filter non-null column values in a Spark DataFrame using a List[String]
:我必须使用
List[String]
过滤 Spark DataFrame 中的非空列值:
val keyList = List("columnA", "columnB", "columnC", "columnD", ...)
For a single column named key
, the syntax should be:对于名为
key
的单个列,语法应为:
val nonNullDf = df.filter(col("key").isNotNull)
My question is how to use the keyList
into the previous filter?我的问题是如何使用
keyList
进入前一个过滤器?
You can generate a filter by doing a map-reduce on keyList
.您可以通过在
keyList
上执行 map-reduce 来生成过滤器。
Use and
if you want to keep the rows where all columns are not null, or use or
if you want to keep the rows where any column is not null.如果要保留所有列都不是 null 的行,请使用
and
, or
如果要保留任何列不是 null 的行,请使用或。
val nonNullDf = df.filter(keyList.map(col(_).isNotNull).reduce(_ and _))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.