Bemærk
Adgang til denne side kræver godkendelse. Du kan prøve at logge på eller ændre mapper.
Adgang til denne side kræver godkendelse. Du kan prøve at ændre mapper.
Returns an array of elements after applying a transformation to each element in the input array. Supports Spark Connect.
For the corresponding Databricks SQL function, see transform function.
Syntax
from pyspark.databricks.sql import functions as dbf
dbf.transform(col=<col>, f=<f>)
Parameters
| Parameter | Type | Description |
|---|---|---|
col |
pyspark.sql.Column or str |
Name of column or expression. |
f |
function |
A function that is applied to each element of the input array. Can take one of the following forms: Unary (x: Column) -> Column or Binary (x: Column, i: Column) -> Column where the second argument is a 0-based index of the element. |
Returns
pyspark.sql.Column: a new array of transformed elements.
Examples
Example 1: Transform array elements with a simple function
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1, [1, 2, 3, 4])], ("key", "values"))
df.select(dbf.transform("values", lambda x: x * 2).alias("doubled")).show()
+------------+
| doubled|
+------------+
|[2, 4, 6, 8]|
+------------+
Example 2: Transform array elements using index
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1, [1, 2, 3, 4])], ("key", "values"))
def alternate(x, i):
return dbf.when(i % 2 == 0, x).otherwise(-x)
df.select(dbf.transform("values", alternate).alias("alternated")).show()
+--------------+
| alternated|
+--------------+
|[1, -2, 3, -4]|
+--------------+