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


Python pyspark map_zip_with用法及代碼示例

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

用法:

pyspark.sql.functions.map_zip_with(col1, col2, f)

使用函數將兩個給定的Mapkey-wise 合並到一個Map中。

版本 3.1.0 中的新函數。

參數

col1 Column 或 str

第一列或表達式的名稱

col2 Column 或 str

第二列或表達式的名稱

f函數

三元函數 (k: Column, v1: Column, v2: Column) -> Column... 可以使用 Column 的方法,在 pyspark.sql.functions 和 Scala 中定義的函數 UserDefinedFunctions 。不支持 Python UserDefinedFunctions (SPARK-27052)。

返回

Column

例子

>>> df = spark.createDataFrame([
...     (1, {"IT": 24.0, "SALES": 12.00}, {"IT": 2.0, "SALES": 1.4})],
...     ("id", "base", "ratio")
... )
>>> df.select(map_zip_with(
...     "base", "ratio", lambda k, v1, v2: round(v1 * v2, 2)).alias("updated_data")
... ).show(truncate=False)
+---------------------------+
|updated_data               |
+---------------------------+
|{SALES -> 16.8, IT -> 48.0}|
+---------------------------+

相關用法


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