How to rename a column in a Dataframe in Apache Spark

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at withColumnRenamed() function in Apache Spark SQL API.

withColumnRenamed(String columnName, String newColumnNameis used to rename a column in a Dataframe.

Let’s look at the below code snippet in spark-shell for renaming a column:

sc.range(1,10).toDF() – creates an RDD with numbers from 1 to 10 and converts the RDD into a Dataframe.

Initially, the column name is shown as _1 and it is renamed to range using withColumnRenamed(“_1”, “range”).

Happy Learning :).

Please let us know in case of any queries.

Leave a Reply

Your email address will not be published.