0

I'm using Pyspark 2.4 and would like to create df_2 from df_1:

df_1:

root
 |-- request: array (nullable = false)
 |    |-- address: struct (nullable = false)
 |    |    |-- street: string (nullable  = false)
 |    |    |-- postcode: string (nullable  = false)

df_2:

root
 |-- request: array (nullable = false)
 |    |-- address: struct (nullable = false)
 |    |    |-- street: string (nullable  = false)

I know UDF is one way, but are there any other ways, like the use of map(), to achieve the same goal?

1 Answer 1

1

Use transform function :

df_2 = df_1.withColumn("request", expr("transform(request, x -> struct(x.street) as address)"))

For each element of request array, we select only street field and create a new struct.

Sign up to request clarification or add additional context in comments.

1 Comment

AnalysisException: cannot resolve 'struct(namedlambdavariable().street )' due to data type mismatch: Only foldable string expressions are allowed to appear at odd position, got: NamePlaceholder;

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.