當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyspark zip_with用法及代碼示例

本文簡要介紹 pyspark.sql.functions.zip_with 的用法。

用法:

pyspark.sql.functions.zip_with(left, right, f)

使用函數將兩個給定的數組按元素合並到一個數組中。如果一個數組較短,則在應用函數之前在末尾附加空值以匹配較長數組的長度。

版本 3.1.0 中的新函數。

參數

left Column 或 str

第一列或表達式的名稱

right Column 或 str

第二列或表達式的名稱

f函數

二進製函數 (x1: Column, x2: Column) -> Column... 可以使用 Column 的方法,在 pyspark.sql.functions 和 Scala 中定義的函數 UserDefinedFunctions 。不支持 Python UserDefinedFunctions (SPARK-27052)。

返回

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]|
+-----------------+

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 pyspark.sql.functions.zip_with。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。