There are multiple ways to rename columns in dataframe using PySpark.
- withColumnRenamed
- df = df.withColumnRenamed("Old_ColumnName1", "New_ColumnName1").withColumnRenamed("Old_ColumnName2", "New_ColumnName2")
- selectExpr
- df = df.selectExpr("Old_ColumnName1 AS NewColumnName1","Old_ColumnName2 AS NewColumnName2")
- select(col().alias(), col())
- df2 = df.select(col("Old_ColumnName1").alias("NewColumnName1"), col("Old_ColumnName2"))
No comments:
Post a Comment