0

Schema

 root
     |-- userId: string (nullable = true)
     |-- languageknowList: array (nullable = true)
     |    |-- element: struct (containsNull = false)
     |    |    |-- code: string (nullable = false)
     |    |    |-- description: string (nullable = false)
     |    |    |-- name: string (nullable = false)

The df has userId and languageknownList. Every user should know English, so English language is not present in languageknowList I have to add.

English
code: 10
description: English Language
name: English

Any one please help me.

1 Answer 1

2

You can create a new array of structs column and concat to the existing column:

import pyspark.sql.functions as F

english = F.struct(F.lit('10').alias('code'),
                   F.lit('English Language').alias('description'), 
                   F.lit('English').alias('name')
                  )

df2 = df.withColumn(
    'languageknowList',
    F.when(
        ~F.array_contains(F.col('languageknowList'), english),
        F.concat(
            F.col('languageknowList'),
            F.array(english)
        )
    ).otherwise(
        F.col('languageknowList')
    )
)
Sign up to request clarification or add additional context in comments.

2 Comments

If it not exist the only need to concatenate
@SuFi sorry, I missed that requirement. See the edited code?

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.