How to convert Array elements to Rows in PySpark ? PySpark – Explode Example code.

PySpark @

Function : pyspark.sql.functions.explode

To converts the Array of Array Columns to row in PySpark we use “explode” function. Explode returns a new row for each element in an array or map. Explodes function usage avoids the loops. Explode returns a new row for each element in the given array or map. Uses the default column name ‘col’ for elements in the array and ‘key’ and ‘value’ for elements in the map unless specified otherwise.

Syntax : explode(col)


from pyspark.sql.functions import explode
raw_data = [
("MLB Network",[["CA","DE","KS","NE"],["ON","PE","QC","SK","YT"]])]
df = spark.createDataFrame(data=raw_data, schema = ['CHANNEL_NM','US_CANADA_STATES'])
df2 =,explode(df.US_CANADA_STATES))


  1. Spark Examples
  2. PySpark Blogs
  3. Bigdata Blogs
  4. Spark Interview Questions
pyspark explode

Spark official page

Author: user

Leave a Reply