0

I have a pyspark dataframe:

Example df:

number  |  matricule<array>   | name<array>  |    
----------------------------------------------
AA      |  []                 |  [7]         |    
----------------------------------------------
AA      |  [9]                |  []         |     
----------------------------------------------
AA      |  [""]                |  [2]         |    
----------------------------------------------
AA      |  [2]                |  [""]      |  

I would like to change the arrays when they have the value string but is empty: [""] to [] I tried by:

df = df.withColumn("matricule_2", F.when(F.col("matricule") == F.lit("[""]"), F.lit("[]")).otherwise(F.col("matricule")))

But I got an error:

AnalysisException: u"cannot resolve, `matricule` = '[]')' due to data type mismatch: differing types.

Expected result:

number  |  matricule<array>   | name<array>  |    
----------------------------------------------
AA      |  []                 |  [7]         |    
----------------------------------------------
AA      |  [9]                |  []          |     
----------------------------------------------
AA      |  []                |  [2]          |    
----------------------------------------------
AA      |  [2]                |  []          |  

Please someone can help me please ? Thank you

3
  • Do you want to convert empty strings as nulls or remove them completely from the array? Commented Feb 24, 2020 at 12:55
  • @blackbishop remove them and keep an empty array [] Commented Feb 24, 2020 at 13:13
  • 1
    If you are on Spark 2.4+, you can use array_remove like this : df = df.withColumn("matricule_2", array_remove(col("matricule"), ""))... Commented Feb 24, 2020 at 13:20

1 Answer 1

1

Dataframe:

+------+---------+----+
|Number|Matricule|Name|
+------+---------+----+
|    AA|     [""]| [7]|
|    AA|      [9]|  []|
|    AA|     [""]| [2]|
|    AA|      [2]|[""]|
+------+---------+----+

Filter out "" from both columns:

df.withColumn("Matricule", F.expr("""filter(Matricule, x -> x!= '""')"""))\
  .withColumn("Name", F.expr("""filter(Name, x -> x!= '""')""")).show()


+------+---------+----+
|Number|Matricule|Name|
+------+---------+----+
|    AA|       []| [7]|
|    AA|      [9]|  []|
|    AA|       []| [2]|
|    AA|      [2]|  []|
+------+---------+----+

As stated in the comments, you can also use array_remove:

df.withColumn("Matricule", F.array_remove("Matricule", '""'))\
  .withColumn("Name", F.array_remove("Name", '""')).show()

+------+---------+----+
|Number|Matricule|Name|
+------+---------+----+
|    AA|       []| [7]|
|    AA|      [9]|  []|
|    AA|       []| [2]|
|    AA|      [2]|  []|
+------+---------+----+
Sign up to request clarification or add additional context in comments.

Comments

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.