Renaming DataFrame Columns in Spark

Updated On March 26, 2021 | By Mahesh Mogal

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.

Renaming DataFrame Columns in Spark

Mahesh Mogal

I am passionate about Cloud, Data Analytics, Machine Learning, and Artificial Intelligence. I like to learn and try out new things. I have started blogging about my experience while learning these exciting technologies.

Stay Updated with Latest Blogs

Get latest blogs delivered to your mail directly.

Recent Posts

Spark Join Types With Examples

In this blog, we are going to learn different spark join types. We will also write code and validate data output for each join type to better understand them.

Spark Join Types With Examples
Read More
Integrate Spark with Jupyter Notebook and Visual Studio Code

In this blog, we are going to integrate spark with jupyter notebook and visual studio code to create easy-to-use development environment.

Integrate Spark with Jupyter Notebook and Visual Studio Code
Read More
Reading Data From SQL Tables in Spark

In this blog, we are going to learn about reading data from SQL tables in Spark. We will create Spark data frames from tables and query results as well.

Reading Data From SQL Tables in Spark
Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
Copy link
Powered by Social Snap