Del via


withField

Add or replace a field in a struct column.

Syntax

withField(fieldName, col)

Parameters

Parameter Type Description
fieldName str Name of the field to add or replace
col Column Column expression for the field value

Returns

Column

Examples

from pyspark.sql import Row
from pyspark.sql.functions import lit
df = spark.createDataFrame([Row(a=Row(b=1, c=2))])
df.withColumn('a', df['a'].withField('b', lit(3))).select('a.b').show()
# +---+
# |  b|
# +---+
# |  3|
# +---+
df.withColumn('a', df['a'].withField('d', lit(4))).select('a.d').show()
# +---+
# |  d|
# +---+
# |  4|
# +---+