pyspark.sql.DataFrame.withColumnRenamed

DataFrame.withColumnRenamed(existing: str, new: str) → pyspark.sql.dataframe.DataFrame[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

string, name of the existing column to rename.

newstr

string, new name of the column.

Returns
DataFrame

DataFrame with renamed column.

Examples

>>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"])
>>> df.withColumnRenamed('age', 'age2').show()
+----+-----+
|age2| name|
+----+-----+
|   2|Alice|
|   5|  Bob|
+----+-----+