named_struct
Creates a struct with the given field names and values.
Syntax
Python
from pyspark.sql import functions as sf
sf.named_struct(*cols)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| List of columns to work on. |
Returns
pyspark.sql.Column:
Examples
Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(1, 2)], ['a', 'b'])
df.select("*", sf.named_struct(sf.lit('x'), df.a, sf.lit('y'), "b")).show()
Output
+---+---+------------------------+
| a| b|named_struct(x, a, y, b)|
+---+---+------------------------+
| 1| 2| {1, 2}|
+---+---+------------------------+