pyspark.sql.DataFrame.withColumnsRenamed¶
-
DataFrame.
withColumnsRenamed
(colsMap: Dict[str, str]) → pyspark.sql.dataframe.DataFrame[source]¶ Returns a new
DataFrame
by renaming multiple columns. This is a no-op if the schema doesn’t contain the given column names.New in version 3.4.0: Added support for multiple columns renaming
- Parameters
- colsMapdict
a dict of existing column names and corresponding desired column names. Currently, only a single map is supported.
- Returns
DataFrame
DataFrame with renamed columns.
See also
Notes
Support Spark Connect
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| +---+-----+----+----+