1

I have a pyspark data frame which has string ,int and array type columns. I am trying to run a for loop for all columns to check if their is any array type column and convert it to string.

The output in the pyspark data frame should then hold the int,string columns.

I have below code however it fails with below error. Any suggestions to fix this error

df2 = df.select([df.withColumn(c,F.concat_ws(",",col(c))).alias(c)  if dict(df.dtypes)[c] in 'array<string>' else F.col(c) for c in df.columns])

Error : For column literals, use 'lit', 'array', 'struct' or 'create_map' function.

1 Answer 1

4

You cannot put withColumn inside select. Try the code below:

import pyspark.sql.functions as F

df2 = df.select([
    F.concat_ws(",", c).alias(c)  
    if dict(df.dtypes)[c] == 'array<string>'
    else F.col(c)  
    for c in df.columns
])
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.