DataFrame.
withColumnsRenamed
Returns a new DataFrame by renaming multiple columns. This is a no-op if the schema doesn’t contain the given column names.
DataFrame
New in version 3.4.0: Added support for multiple columns renaming
Changed in version 3.4.0: Supports Spark Connect.
a dict of existing column names and corresponding desired column names. Currently, only a single map is supported.
DataFrame with renamed columns.
See also
withColumnRenamed()
Examples
>>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"]) >>> df = df.withColumns({'age2': df.age + 2, 'age3': df.age + 3}) >>> df.withColumnsRenamed({'age2': 'age4', 'age3': 'age5'}).show() +---+-----+----+----+ |age| name|age4|age5| +---+-----+----+----+ | 2|Alice| 4| 5| | 5| Bob| 7| 8| +---+-----+----+----+