withField
Ajoutez ou remplacez un champ dans une colonne de struct.
Syntaxe
Python
withField(fieldName, col)
parameter
parameter | Type | Description |
|---|---|---|
| str | Nom du champ à ajouter ou à remplacer |
| Colonne | Expression de colonne pour la valeur du champ |
Renvoie
Colonne
Exemples
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|
# +---+