在spark dataframe中查找非空值的计数

hmae6n7t  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(812)

我试过了 df.describe().filter($"summary" === "count").show 但这只适用于整数。我尝试了如下的for循环:

import scala.collection.mutable.ListBuffer
var count_val = ListBuffer[Long]()

for (i<-0 to column_names.length) {
    count_val += df.select(column_names(i)).where(column_names(i)+" is not null").count
}

还有其他更快的方法吗?dataframe的类型为org.apache.spark.sql.dataframe。

gpnt7bae

gpnt7bae1#

使用 def count(e: org.apache.spark.sql.Column): org.apache.spark.sql.Column &此函数将返回not null值的计数。
它会给你和我一样的结果 df.describe() 为了 count .

df.select(df.columns.map(c => count(col(c)).as(c)):_*).show(false)

Spark 2.4.2 版本 df.describe().filter($"summary" === "count").show 正在处理类型为的列 string .

相关问题