close

How to change dataframe column names in pyspark?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to change dataframe column names in pyspark in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to change dataframe column names in pyspark?

  1. How to change dataframe column names in pyspark?

    If you want to change all columns names, try df.toDF(*cols)

  2. change dataframe column names in pyspark

    If you want to change all columns names, try df.toDF(*cols)

Method 1

df = df.withColumnRenamed("colName", "newColName")\
       .withColumnRenamed("colName2", "newColName2")

Advantage of using this way: With long list of columns you would like to change only few column names. This can be very convenient in these scenarios. Very useful when joining tables with duplicate column names.

Method 2

If you want to change all columns names, try df.toDF(*cols)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read