withField

Ajoutez ou remplacez un champ dans une colonne de struct.

Syntaxe

withField(fieldName, col)

Paramètres

Paramètre Type Description
fieldName str Nom du champ à ajouter ou remplacer
col Colonne Expression de colonne pour la valeur du champ

Retours

Colonne

Exemples

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|
# +---+