pyspark.sql.DataFrame.withColumnRenamed#

DataFrame.withColumnRenamed(existing, new)[source]#

Returns a new DataFrame by renaming an existing column. This is a no-op if the schema doesn’t contain the given column name.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
existingstr

The name of the existing column to be renamed.

newstr

The new name to be assigned to the column.

Returns
DataFrame

A new DataFrame with renamed column.

Examples

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

Example 1: Rename a single column

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

Example 2: Rename a column that does not exist (no-op)

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

Example 3: Rename multiple columns

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