How does PySpark and scala rename a DataFrame column?

admin

6/19/2023
All Articles

#spark #scala #program

How does PySpark and scala  rename a DataFrame column?

How does PySpark and spark scala rename a DataFrame column?

 

There is way to rename filed in spark :

Using pyspark expression we can rename filed  value

 df = data.selectExpr("Name as name", "birthday as age")
 df.show()
 df.printSchema()

 

Using  method withColumnRenamed  in scala

df.withColumnRenamed("dob","DateOfBirth")

    .printSchema() 
 
 
Usig alise method 
 
 from pyspark.sql.functions import col

 data = data.select(col("Name").alias("name"), col("birthday").alias("age"))
 data.show()