Skip to main content

withField

Add or replace a field in a struct column.

Syntax​

Python
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

Parameter

Type

Description

fieldName

str

Name of the field to add or replace

col

Column

Column expression for the field value

Returns​

Column

Examples​

Python
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()
Output
# +---+
# | b|
# +---+
# | 3|
# +---+
Python
df.withColumn('a', df['a'].withField('d', lit(4))).select('a.d').show()
Output
# +---+
# | d|
# +---+
# | 4|
# +---+