如何在 PySpark 中过滤数组列中的值?

Vzz*_*arr 1 apache-spark pyspark

我在 Pyspark 中有一个 ArrayType 列。我只想过滤数组中每一行的值(我不想过滤掉实际的行!)而不使用 UDF。

例如,给定此数据集,其中 A 列为 ArrayType:

|     A      |
______________
|[-2, 1, 7]  |
|[1]         |
|[-4, -1, -3]|
Run Code Online (Sandbox Code Playgroud)

我只想输出正值:

|     A      |
______________
|[1, 7]      |
|[1]         |
|[]          |
Run Code Online (Sandbox Code Playgroud)

Sur*_*ali 6

对于 Spark 2.4 及更高版本,

from pyspark.sql.functions import expr

df.withColumn("A", expr("filter(A, x -> x > 0)")).show()
Run Code Online (Sandbox Code Playgroud)