pyspark.sql.functions.explode_outer#
- pyspark.sql.functions.explode_outer(col)[source]#
Returns a new row for each element in the given array or map. Unlike explode, if the array/map is null or empty then null is produced. Uses the default column name col for elements in the array and key and value for elements in the map unless specified otherwise.
Added in version 2.3.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters:
- col
Columnor column name target column to work on. A column that evaluates to an array or map.
- col
- Returns:
Columnone row per array item or map key value. Returns a column of the element type of the input array, or the key and value columns of the input map.
See also
Examples
Example 1: Using 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.select('*', sf.explode_outer('a')).show() +---+---------------+----+ | i| a| col| +---+---------------+----+ | 1|[1, 2, 3, NULL]| 1| | 1|[1, 2, 3, NULL]| 2| | 1|[1, 2, 3, NULL]| 3| | 1|[1, 2, 3, NULL]|NULL| | 2| []|NULL| | 3| NULL|NULL| +---+---------------+----+
Example 2: Using 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.select('*', sf.explode_outer('m')).show(truncate=False) +---+---------------------------+----+-----+ |i |m |key |value| +---+---------------------------+----+-----+ |1 |{1 -> 2, 3 -> 4, 5 -> NULL}|1 |2 | |1 |{1 -> 2, 3 -> 4, 5 -> NULL}|3 |4 | |1 |{1 -> 2, 3 -> 4, 5 -> NULL}|5 |NULL | |2 |{} |NULL|NULL | |3 |NULL |NULL|NULL | +---+---------------------------+----+-----+