vll*_*990 5 dataframe apache-spark apache-spark-sql pyspark
我需要找到一种方法来获取nullpyspark 数据框中具有值的所有行。
例如,我有以下日期范围:
   +-------+-------+-------+
   |   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 ...
您可以使用 pythonfunctools.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())