nxcals.api.custom.data.services.DataFrame.withColumnRenamed
- DataFrame.withColumnRenamed(existing: str, new: str) DataFrame
Returns a new
DataFrameby renaming an existing column. This is a no-op if the schema doesn’t contain the given column name.Added in version 1.3.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters:
existing (str) – The name of the existing column to be renamed.
new (str) – The new name to be assigned to the column.
- Returns:
A new DataFrame with renamed column.
- Return type:
See also
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| +----+-----+