withColumnRenamed – How to Rename a column in PySpark?

Spread the love

In this post you will learn how to rename a column in pyspark.

withColumnRenamed –

To Rename a column in PySpark we can use the withColumnRenamed method. This will rename the column with the name of the string in the first argument to the string in the second argument.

Let’s read a dataset to illustrate it. We will use the restaurant dataset.

from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()

df = spark.read.format('csv').option('header','true').load('../data/Restaurant.csv')

Let’s say we want to rename the Meal price column. We want to remove the dollar sign.

df = df.withColumnRenamed("Meal Price ($)", "Meal Price")

Rating: 1 out of 5.

Leave a Reply