Spark withColumnRenamed to Rename Column

  • Post author:
  • Post category:Apache Spark

In Spark withColumnRenamed() is used to rename one column or multiple DataFrame column names. Depends on the DataFrame schema, renaming columns might get simple to complex, especially when a column is nested with struct type it gets complicated.

In this article, I will explain how to rename a DataFrame column with multiple use cases like rename selected multiple columns, nested struct columns, all columns with Scala examples.

  1. Using withColumnRenamed – To rename Spark DataFrame column name
  2. Using withColumnRenamed – To rename multiple columns
  3. Using StructType – To rename nested column on Spark DataFrame
  4. Using Select – To rename nested columns
  5. Using withColumn – To rename nested columns
  6. Using col() function – To Dynamically rename all or multiple columns
  7. Using toDF() – To rename all or multiple columns

First, let’s create our data for our examples, we are using Row class as we convert this data to Spark DataFrame.

val data = Seq(Row(Row("James ","","Smith"),"36636","M",3000),
  Row(Row("Michael ","Rose",""),"40288","M",4000),
  Row(Row("Robert ","","Williams"),"42114","M",4000),
  Row(Row("Maria ","Anne","Jones"),"39192","F",4000),

Our base schema with nested structure.

val schema = new StructType()
  .add("name",new StructType()

Let’s create the DataFrame by using parallelize and provide the above schema.

val df = spark.createDataFrame(spark.sparkContext.parallelize(data),schema)

Below is our schema structure. I am not printing data here as it is not necessary for our examples. This schema has a nested structure.

Spark dataframe rename column

1. Using Spark withColumnRenamed – To rename DataFrame column name

Spark has a withColumnRenamed() function on DataFrame to change a column name. This is the most straight forward approach; this function takes two parameters; the first is your existing column name and the second is the new column name you wish for.


def withColumnRenamed(existingName: String, newName: String): DataFrame

existingName – The existing column name you want to change

newName – New name of the column

Returns a new DataFrame (Dataset[Row]) with a column renamed. This is a no-op if the schema doesn’t contain existingName.



The above example changes the column name from “dob” to “DateOfBirth” on spark DataFrame. Note that withColumnRenamed function returns a new DataFrame and doesn’t modify the current DataFrame.

Spark dataframe rename column

2. Using withColumnRenamed – To rename multiple columns

To change multiple column names, we should chain withColumnRenamed functions as shown below.

val df2 = df.withColumnRenamed("dob","DateOfBirth")

This creates a new DataFrame “df2” after renaming dob and salary columns.

3. Using Spark StructType – To rename a nested column in Dataframe

Changing a column name on nested data is not straight forward and we can do this by creating a new schema with new DataFrame columns using StructType and use it using cast function as shown below.

val schema2 = new StructType()

This statement renames firstname to fname and lastname to lname within name structure.

rename multiple columns

4. Using Select – To rename nested elements.

Let’s see another way to change nested columns by transposing the structure to flat."name.firstname").as("fname"),
Spark rename nested columns

5. Using Spark DataFrame withColumn – To rename nested columns

When you have nested columns on Spark DatFrame and if you want to rename it, use withColumn on a data frame object to create a new column from an existing and we will need to drop the existing column. Below example creates a “fname” column from “name.firstname” and drops the “name” column

val df4 = df.withColumn("fname",col("name.firstname"))

6. Using col() function – To Dynamically rename all or multiple columns

Another way to change all column names on Dataframe is to use col() function.

val old_columns = Seq("dob","gender","salary","fname","mname","lname")
    val new_columns = Seq("DateOfBirth","Sex","salary","firstName","middleName","lastName")
    val columnsList =>{col(f._1).as(f._2)})
    val df5 =*)

7. Using toDF() – To change all columns in a Spark DataFrame

When we have data in a flat structure (without nested) , use toDF() with a new schema to change all column names.

val newColumns = Seq("newCol1","newCol2","newCol3")
val df = df.toDF(newColumns:_*)

The complete code can be downloaded from GitHub


This article explains withColumnRenamed() function and different ways to rename a single column, multiple, all, and nested columns on Spark DataFrame. Besides what explained here, we can also change column names using Spark SQL and the same concept can be used in PySpark.

Hope you like this article!!

Happy Learning.

NNK is a BigData and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment using Scala and Maven.

Leave a Reply