I have to calculate the total number of null values in my dataset including all rows and all columns and the output must be an integer that would represent the total number of null values in my dataset.
I have to calculate the total number of null values in my dataset including all rows and all columns and the output must be an integer that would represent the total number of null values in my dataset.
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]
Test:
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