Renaming DataFrame Columns in Spark

Many times we have to change column names in our data. Either the existing column name is too long or too short or not descriptive enough to understand what data we are accessing. In this blog, we are going to learn about renaming data frame columns in spark. There are many ways you can do this and you can choose whatever best fits for your needs.

Using withColumnRenamed

Before we start, let us create data frame for to work with.

Spark has provided us in built function when we want rename columns. Let us see how we can use this.

We can also rename multiple columns at once by chaining withColumnRenamed calls.

Using withColumn

Though “withColumn” function is used to add new columns to Spark data frame, we can also use it to rename columns as well. We need to pass expression to select old column value when using “withColumn”. Let us check how we can do it.

We can either use “col” or “expr” function to pass whichever column we want to rename with this function. But there is one issue when using “withColumn”. Our exiting column is still there in the data frame. To handle this condition, we can drop that column while renaming it.

Now we are getting the desired result. We have deleted old columns in the data frame while renaming them.

Using Select Expression to Rename Columns

Spark data frames act much like SQL statements in most cases. Like SQL, we can also rename columns using “SELECT” or “SELECTEXPR” functions in Spark. This is really simple to understand if you are familiar with SQL queries.

We can also use select statement to rename columns.

Conclusion

Renaming columns is one of frequent things we need to do while processing data with Spark. It is a good thing that we have so many easy options to achieve this. You can find code written in this blog at GitHub. I hope you will find this useful. Keep Learning.

Similar Posts