How to convert empty arrays to nulls?

后端 未结 7 541
Happy的楠姐
Happy的楠姐 2021-01-13 18:28

I have below dataframe and i need to convert empty arrays to null.

+----+---------+-----------+
|  id|count(AS)|count(asdr)|
+----+---------+-----------+
|11         


        
7条回答
  •  情书的邮戳
    2021-01-13 19:05

    You need to check for the size of the array type column. Like:

    df.show()
    +----+---+
    |  id|arr|
    +----+---+
    |1110| []|
    +----+---+
    
    df.withColumn("arr", when(size(col("arr")) == 0 , lit(None)).otherwise(col("arr") ) ).show()
    
    +----+----+
    |  id| arr|
    +----+----+
    |1110|null|
    +----+----+
    

提交回复
热议问题