How to rename multiple columns in PySpark dataframe ? Last Updated : 04 Jul, 2021 Comments Improve Suggest changes 1 Likes Like Report In this article, we are going to see how to rename multiple columns in PySpark Dataframe. Before starting let's create a dataframe using pyspark: Python3 # importing module import pyspark from pyspark.sql.functions import col # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an app name spark = SparkSession.builder.appName('sparkdf').getOrCreate() # list of students data data = [["1", "sravan", "vignan"], ["2", "ojaswi", "vvit"], ["3", "rohith", "vvit"], ["4", "sridevi", "vignan"], ["1", "sravan", "vignan"], ["5", "gnanesh", "iit"]] # specify column names columns = ['student ID', 'student NAME', 'college'] # creating a dataframe from the lists of data dataframe = spark.createDataFrame(data, columns) print("Actual data in dataframe") # show dataframe dataframe.show() Output: Method 1: Using withColumnRenamed. Here we will use withColumnRenamed() to rename the existing columns name. Syntax: withColumnRenamed( Existing_col, New_col) Parameters: Existing_col: Old column name.New_col: New column name. Example 1: Renaming single columns. Python3 dataframe.withColumnRenamed("college", "College Name").show() Output: Example 2: Renaming multiple columns. Python3 df2 = dataframe.withColumnRenamed("student ID", "Id").withColumnRenamed("college", "College_Name") df2.show() Output: Method 2: Using toDF() This function returns a new DataFrame that with new specified column names. Syntax: toDF(*col) Where, col is a new column name In this example, we will create an order list of new column names and pass it into toDF function. Python3 Data_list = ["College Id"," Name"," College"] new_df = dataframe.toDF(*Data_list) new_df.show() Output: Create Quiz Comment K kumar_satyam Follow 1 Improve K kumar_satyam Follow 1 Improve Article Tags : Python Python-Pyspark Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like