Skip to main content

TableValuedFunction.posexplode

Returns a DataFrame containing a new row for each element with position in the given array or map. Uses the default column name pos for position, and col for elements in the array and key and value for elements in the map unless specified otherwise.

Syntax

Python
spark.tvf.posexplode(collection)

Parameters

Parameter

Type

Description

collection

pyspark.sql.Column

Target column to work on.

Returns

pyspark.sql.DataFrame: A DataFrame with a new row for each element along with its position.

Examples

Python
import pyspark.sql.functions as sf
spark.tvf.posexplode(sf.array(sf.lit(1), sf.lit(2), sf.lit(3))).show()
Output
+---+---+
|pos|col|
+---+---+
| 0| 1|
| 1| 2|
| 2| 3|
+---+---+
Python
import pyspark.sql.functions as sf
spark.tvf.posexplode(sf.create_map(sf.lit("a"), sf.lit("b"))).show()
Output
+---+---+-----+
|pos|key|value|
+---+---+-----+
| 0| a| b|
+---+---+-----+