My question is relevant to this, but it got a new problem.
Why the empty array has non-zero size ?
import pyspark.sql.functions as F
import pyspark.sql.types as T
new_customers = spark.createDataFrame(data=[["Karen", ["a"]], ["Penny", ["b"]], ["John", [None]], ["Cosimo", ["d"]]], schema=["name", "val"])
new_customers.printSchema()
new_customers.show(5, False)
new_customers = new_customers.withColumn("new_val", F.size("val"))
new_customers.show(10, truncate=False)
The results:
root
|-- name: string (nullable = true)
|-- val: array (nullable = true)
| |-- element: string (containsNull = true)
+------+---+
|name |val|
+------+---+
|Karen |[a]|
|Penny |[b]|
|John |[] |
|Cosimo|[d]|
+------+---+
+------+---+-------+
|name |val|new_val|
+------+---+-------+
|Karen |[a]|1 |
|Penny |[b]|1 |
|John |[] |1 | <- # why it is 1 ?
|Cosimo|[d]|1 |
+------+---+-------+
pyspark version is 2.3.2
thanks