close
0

How to change dataframe column names in pyspark?

avatar
asked 23-06-2022

4 Answers


0

Try

df = df.withColumnRenamed("colName", "newColName")\
       .withColumnRenamed("colName2", "newColName2")
avatar
answered 23-06-2022

0

Use 

new_column_name_list= list(map(lambda x: x.replace(" ", "_"), df.columns))

df = df.toDF(*new_column_name_list)
avatar
answered 23-06-2022

0

Use 

df.withColumnRenamed('age', 'age2')

avatar
answered 23-06-2022

0

Try

df = df.select( '*', F.col('count').alias('new_count') ).drop('count')
avatar
answered 23-06-2022