Share via


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

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

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