Created
June 11, 2020 06:58
-
-
Save kovid-r/bc88ac6ee1a87cd0b2dc62e6243b8001 to your computer and use it in GitHub Desktop.
Different methods for Changing column name PySpark Cheatsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Changing column name with withColumnRenamed feature | |
df = df.withColumnRenamed('existing_column_name', 'new_column_name') | |
# Changing column with selectExpr (you'll have to select all the columns here) | |
df = df.selectExpr("existing_column_name AS existing_1", "new_column_name AS new_1") | |
# Changing column with sparksql functions - col and alias | |
from pyspark.sql.functions import col | |
df = df.select(col("existing_column_name").alias("existing_1"), col("new_column_name").alias("new_1")) | |
# Changing column with a SQL select statement | |
sqlContext.registerDataFrameAsTable(df, "df_table") | |
df = sqlContext.sql("SELECT existing_column_name AS existing_1, new_column_name AS new_1 FROM df_table") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment