0

I've Spark DataFrame with a Array column (StringType)

Sample DataFrame:

df = spark.createDataFrame([
  [None],   
  [[]],   
  [['foo']] 
]).toDF("a")

Current Output:

+-----+
|    a|
+-----+
| null|
|   []|
|[foo]|
+-----+

Desired Output:

+-----+
|    a|
+-----+
|   []|
|   []|
|[foo]|
+-----+

I need to convert the Null values to an empty Array to concat with another array column.

Already tried this, but it's not working

df.withColumn("a",F.coalesce(F.col("a"),F.from_json(F.lit("[]"), T.ArrayType(T.StringType()))))

Convert null values to empty array in Spark DataFrame

1
  • Great question! Commented Aug 13, 2020 at 16:41

1 Answer 1

3

Use array function.

df = spark.createDataFrame([
  [None],   
  [[]],   
  [['foo']] 
]).toDF("a")

import pyspark.sql.functions as F

df.withColumn('a', F.coalesce(F.col('a'), F.array(F.lit(None)))).show(10, False)
+-----+
|a    |
+-----+
|[]   |
|[]   |
|[foo]|
+-----+

The result is now array(string), so there is no null value. Please check the results.

temp = spark.sql("SELECT a FROM table WHERE a is NULL")
temp.show(10, False)
temp = spark.sql("SELECT a FROM table WHERE a = array(NULL)")
temp.show(10, False)
temp = spark.sql("SELECT a FROM table")
temp.show(10, False)


+---+
|a  |
+---+
+---+

+---+
|a  |
+---+
|[] |
+---+

+-----+
|a    |
+-----+
|[]   |
|[]   |
|[foo]|
+-----+
Sign up to request clarification or add additional context in comments.

11 Comments

I still have Null values. Column is ArrayType(StringType) Does that make sense?
what version of spark, python?
Spark 3 and Python 3.6
Even my code is for spark 3.0.0 and python 3.8.5. I think the spark version is the matter, so it should work. I have added my full code.
Results are not displayed properly (Presto/Superset) . After export and verification of the CSV everything appears to be correct. Thank you very much for your patience and help !!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.