如何在pyspark中的任何列中获取所有具有空值的行
How to get all rows with null value in any column in pyspark
我需要找到一种方法来获取 pyspark 数据框中具有 null
值的所有行。
例如,我有以下日期框:
+-------+-------+-------+
| c_00| c_01| c_02|
+-------+-------+-------+
1 | null | 0.141| 0.141|
2 | 0.17| 0.17| 0.17|
3 | 0.25| null| 0.25|
4 | 0.135| 0.135| 0.135|
我想要在任何列中包含 null
个值的所有行
+-------+-------+-------+
| c_00| c_01| c_02|
+-------+-------+-------+
1 | null | 0.141| 0.141|
3 | 0.25| null| 0.25|
通过链接多个 OR 条件进行过滤 c_00 is null or c_01 is null OR ...
您可以使用 python functools.reduce
从数据框列动态构建过滤器表达式:
from functools import reduce
from pyspark.sql import functions as F
df = spark.createDataFrame([
(None, 0.141, 0.141), (0.17, 0.17, 0.17),
(0.25, None, 0.25), (0.135, 0.135, 0.135)
], ["c_00", "c_01", "c_02"])
cols = [F.col(c) for c in df.columns]
filter_expr = reduce(lambda a, b: a | b.isNull(), cols[1:], cols[0].isNull())
df.filter(filter_expr).show()
#+----+-----+-----+
#|c_00| c_01| c_02|
#+----+-----+-----+
#|null|0.141|0.141|
#|0.25| null| 0.25|
#+----+-----+-----+
或将数组与 exists
函数一起使用:
filter_expr = F.exists(F.array(*df.columns), lambda x: x.isNull())
我需要找到一种方法来获取 pyspark 数据框中具有 null
值的所有行。
例如,我有以下日期框:
+-------+-------+-------+
| c_00| c_01| c_02|
+-------+-------+-------+
1 | null | 0.141| 0.141|
2 | 0.17| 0.17| 0.17|
3 | 0.25| null| 0.25|
4 | 0.135| 0.135| 0.135|
我想要在任何列中包含 null
个值的所有行
+-------+-------+-------+
| c_00| c_01| c_02|
+-------+-------+-------+
1 | null | 0.141| 0.141|
3 | 0.25| null| 0.25|
通过链接多个 OR 条件进行过滤 c_00 is null or c_01 is null OR ...
您可以使用 python functools.reduce
从数据框列动态构建过滤器表达式:
from functools import reduce
from pyspark.sql import functions as F
df = spark.createDataFrame([
(None, 0.141, 0.141), (0.17, 0.17, 0.17),
(0.25, None, 0.25), (0.135, 0.135, 0.135)
], ["c_00", "c_01", "c_02"])
cols = [F.col(c) for c in df.columns]
filter_expr = reduce(lambda a, b: a | b.isNull(), cols[1:], cols[0].isNull())
df.filter(filter_expr).show()
#+----+-----+-----+
#|c_00| c_01| c_02|
#+----+-----+-----+
#|null|0.141|0.141|
#|0.25| null| 0.25|
#+----+-----+-----+
或将数组与 exists
函数一起使用:
filter_expr = F.exists(F.array(*df.columns), lambda x: x.isNull())