I have a dataframe that i want to make a unionAll with a nother dataframe. The problem is that the second dataframe has thre more columns than the first one. Is there a way for me to add three columns with only empty cells in my first dataframe?
Asked
Active
Viewed 1.3k times
3
-
1Possible duplicate of [Add an empty column to spark DataFrame](http://stackoverflow.com/questions/33038686/add-an-empty-column-to-spark-dataframe) – zero323 Feb 09 '16 at 15:40
3 Answers
2
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()))

Vishwajeet Pol
- 43
- 7
-
1Just an observation: in your second example, the imported type does not match with the converted type used. – Itagyba Abondanza Kuhlmann Jul 13 '20 at 19:28
1
use withColumn
object of spark-dataframe
DF.withColumn("NewCol","Value")

Hossein Vatani
- 1,381
- 14
- 26
-
-
got it to work with .withColumn('NewCol', lit(None).cast(StringType())) – dagrun Feb 09 '16 at 13:20
-