pyspark.sql.functions.posexplode#
- pyspark.sql.functions.posexplode(col)[source]#
Returns 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.
New in version 2.1.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- col
Column
or column name target column to work on.
- col
- Returns
Column
one row per array item or map key value including positions as a separate column.
See also
Examples
Example 1: Exploding an array column
>>> from pyspark.sql import functions as sf >>> df = spark.sql('SELECT * FROM VALUES (1,ARRAY(1,2,3,NULL)), (2,ARRAY()), (3,NULL) AS t(i,a)') >>> df.show() +---+---------------+ | i| a| +---+---------------+ | 1|[1, 2, 3, NULL]| | 2| []| | 3| NULL| +---+---------------+
>>> df.select('*', sf.posexplode('a')).show() +---+---------------+---+----+ | i| a|pos| col| +---+---------------+---+----+ | 1|[1, 2, 3, NULL]| 0| 1| | 1|[1, 2, 3, NULL]| 1| 2| | 1|[1, 2, 3, NULL]| 2| 3| | 1|[1, 2, 3, NULL]| 3|NULL| +---+---------------+---+----+
Example 2: Exploding a map column
>>> from pyspark.sql import functions as sf >>> df = spark.sql('SELECT * FROM VALUES (1,MAP(1,2,3,4,5,NULL)), (2,MAP()), (3,NULL) AS t(i,m)') >>> df.show(truncate=False) +---+---------------------------+ |i |m | +---+---------------------------+ |1 |{1 -> 2, 3 -> 4, 5 -> NULL}| |2 |{} | |3 |NULL | +---+---------------------------+
>>> df.select('*', sf.posexplode('m')).show(truncate=False) +---+---------------------------+---+---+-----+ |i |m |pos|key|value| +---+---------------------------+---+---+-----+ |1 |{1 -> 2, 3 -> 4, 5 -> NULL}|0 |1 |2 | |1 |{1 -> 2, 3 -> 4, 5 -> NULL}|1 |3 |4 | |1 |{1 -> 2, 3 -> 4, 5 -> NULL}|2 |5 |NULL | +---+---------------------------+---+---+-----+