pyspark.sql.Column.transform#

Column.transform(f)[source]#

Applies a transformation function to this column.

This method allows you to apply a function that takes a Column and returns a Column, enabling method chaining and functional transformations.

New in version 4.1.0.

Parameters
fcallable

A function that takes a Column and returns a Column.

Returns
Column

The result of applying the function to this column.

Examples

Example 1: Chain built-in functions

>>> from pyspark.sql.functions import trim, upper
>>> df = spark.createDataFrame([("  hello  ",), ("  world  ",)], ["text"])
>>> df.select(df.text.transform(trim).transform(upper).alias("result")).show()
+------+
|result|
+------+
| HELLO|
| WORLD|
+------+

Example 2: Use lambda functions

>>> df = spark.createDataFrame([(10,), (20,), (30,)], ["value"])
>>> df.select(
...     df.value.transform(lambda c: c + 5)
...     .transform(lambda c: c * 2)
...     .transform(lambda c: c - 10).alias("result")
... ).show()
+------+
|result|
+------+
|    20|
|    40|
|    60|
+------+