Преобразовать значения null
в true
/ false
, затем в целые числа, а затем сложить их:
from pyspark.sql import functions as F
from pyspark.sql.types import IntegerType
df = spark.createDataFrame([[1, None, None, 0],
[2, 3, 4, None],
[None, None, None, None],
[1, 5, 7, 2]], 'a: int, b: int, c: int, d: int')
df.select(sum([F.isnull(df[col]).cast(IntegerType()) for col in df.columns]).alias('null_count')).show()
Выход:
+----------+
|null_count|
+----------+
| 2|
| 1|
| 4|
| 0|
+----------+