Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add empty column to dataframe in Spark with python

I have a dataframe that I want to make a unionAll with another dataframe. The problem is that the second dataframe has three more columns than the first one. Is there a way for me to add three columns with only empty cells in my first dataframe?

like image 227
dagrun Avatar asked Dec 02 '25 20:12

dagrun


1 Answers

Maybe this will help

To add string type column:

from pyspark.sql.types import StringType

df.withColumn("COL_NAME", lit(None).cast(StringType()))

To Add integer type

from pyspark.sql.types import IntegerType

df.withColumn("COL_NAME", lit(0).cast(IntegerType()))
like image 110
Vishwajeet Pol Avatar answered Dec 04 '25 08:12

Vishwajeet Pol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!