pyspark.sql.functions.zip_with

pyspark.sql.functions. zip_with ( left : ColumnOrName , right : ColumnOrName , f : Callable [ [ pyspark.sql.column.Column , pyspark.sql.column.Column ] , pyspark.sql.column.Column ] ) → pyspark.sql.column.Column [source]

合并两个给定的数组,逐元素地,使用一个函数将它们合并成一个数组。如果一个数组较短,则在应用函数之前,在末尾追加null以匹配较长数组的长度。

新增于版本 3.1.0。

在版本 3.4.0 中更改: 支持 Spark Connect。

Parameters
left Column or str

第一列或表达式的名称

right Column or str

第二列的名称或表达式

f function

一个二元函数 (x1: Column, x2: Column) -> Column... 可以使用 Column 的方法、在 pyspark.sql.functions 中定义的函数和 Scala UserDefinedFunctions 。 Python UserDefinedFunctions 不受支持 ( SPARK-27052 )。

Returns
Column

通过将给定函数应用于每对参数而得出的计算值数组。

示例

>>> df = spark.createDataFrame([(1, [1, 3, 5, 8], [0, 2, 4, 6])], ("id", "xs", "ys"))
>>> df.select(zip_with("xs", "ys", lambda x, y: x ** y).alias("powers")).show(truncate=False)
+---------------------------+
|powers                     |
+---------------------------+
|[1.0, 9.0, 625.0, 262144.0]|
+---------------------------+
>>> df = spark.createDataFrame([(1, ["foo", "bar"], [1, 2, 3])], ("id", "xs", "ys"))
>>> df.select(zip_with("xs", "ys", lambda x, y: concat_ws("_", x, y)).alias("xs_ys")).show()
+-----------------+
|            xs_ys|
+-----------------+
|[foo_1, bar_2, 3]|
+-----------------+