【发布时间】:2022-09-22 13:33:33
【问题描述】:
我必须计算我的数据集中所有行和所有列的空值总数,并且输出必须是一个整数,表示我的数据集中空值的总数。
标签: apache-spark pyspark apache-spark-sql null bigdata
我必须计算我的数据集中所有行和所有列的空值总数,并且输出必须是一个整数,表示我的数据集中空值的总数。
标签: apache-spark pyspark apache-spark-sql null bigdata
null_cnt = df.select(
F.aggregate(
F.array(*[F.sum(F.when(F.isnull(c), 1)) for c in df.columns]),
F.expr("0L"),
lambda sum, x: sum + x
)
).head()[0]
测试:
from pyspark.sql import functions as F
df = spark.createDataFrame([(1, 2), (None, None), (3, 4)], ['col1', 'col2'])
null_cnt = df.select(
F.aggregate(
F.array(*[F.sum(F.when(F.isnull(c), 1)) for c in df.columns]),
F.expr("0L"),
lambda sum, x: sum + x
)
).head()[0]
print(null_cnt)
# 2
【讨论】: