1

I'm currently dealing with the following error while trying to run pyspark.sql.functions.explode on a array column in a DataFrame in PySpark. I've tried creating a UDF to convert the column to a python list if it is not a list instance. However, this still throws the same error. In Pandas I'd typically pull out the row and determine what to do from there. I'm not sure how I can access this row to look at the data to understand what conditions I need to account for.

I'm more looking for debugging advice in general, but if you know the answer that's great too!

Py4JJavaError: An error occurred while calling o2850.withColumn. : org.apache.spark.sql.AnalysisException: cannot resolve 'explode(lot)' due to data type mismatch: input to function explode should be array or map type, not LongType;;

df.schema()

root
 |-- lists: array (nullable = true)
 |    |-- element: long (containsNull = true)
 |-- data: string (nullable=true)

df = df.withColumn("list",df.lists)
df = df.withColumn('list',sf.explode(df.list))

Original Code

from pyspark.sql import functions as sf 

# create duplicate column to use with explode 
# explode the array datetype into multiple rows per element 

df = spark.read("s3a://path/parquet/*")     
df = df.withColumn("list",df.lists)
df = df.withColumn('list',sf.explode(df.list)) 
2
  • can you please post the code snippet of the problem, that will be much helpful in debugging or finding out the probelem Commented Nov 21, 2017 at 9:23
  • please, do not post code snippets in the comments! Edit your post instead... Commented Nov 21, 2017 at 21:35

1 Answer 1

1

There is no need to used withcolumn you can directly explode the array.

df = spark.read("s3a://path/parquet/*")     
df.select(df['data'],explode(df['lists']).alias('list'))
Sign up to request clarification or add additional context in comments.

1 Comment

if you want original lists column with this then you can also select lists.

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.