cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How do I add a new column to a Spark DataFrame (using PySpark)?

Kaniz
Community Manager
Community Manager
 
1 REPLY 1

saipujari_spark
Valued Contributor
Valued Contributor

We can add a new column using the withColumn() method of the data frame, like below

from pyspark.sql.functions import lit
 
df = sqlContext.createDataFrame(
    [(1, "a"), (2, "b")], ("c1", "c2"))
 
df_new_col = df.withColumn("c3", lit(0))
df_new_col.show()

Thanks,
Saikrishna Pujari
Sr. Spark Technical Solutions Engineer, Databricks
Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.