Aller au contenu principal

withColumnRenamed

Renvoie un nouveau DataFrame en renommant une colonne existante. C'est une aucune opération si le schéma ne contient pas le nom de colonne donné.

Syntaxe​

withColumnRenamed(existing: str, new: str)

parameter​

parameter

Type

Description

existing

str

Le nom de la colonne existante à renommer.

new

str

Le nouveau nom à attribuer à la colonne.

parameter

Type

Description

existing

str

Le nom de la colonne existante à renommer.

new

str

Le nouveau nom à attribuer à la colonne.

Renvoie​

DataFrameUn nouveau DataFrame avec colonne renommée.

Exemples​

Python
df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"])

df.withColumnRenamed("age", "age2").show()
# +----+-----+
# |age2| name|
# +----+-----+
# | 2|Alice|
# | 5| Bob|
# +----+-----+

df.withColumnRenamed("non_existing", "new_name").show()
# +---+-----+
# |age| name|
# +---+-----+
# | 2|Alice|
# | 5| Bob|
# +---+-----+

df.withColumnRenamed("age", "age2").withColumnRenamed("name", "name2").show()
# +----+-----+
# |age2|name2|
# +----+-----+
# | 2|Alice|
# | 5| Bob|
# +----+-----+