pyspark.sql.functions.explode¶
-
pyspark.sql.functions.
explode
(col: ColumnOrName) → pyspark.sql.column.Column[source]¶ 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.
New in version 1.4.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- col
Column
or str target column to work on.
- col
- Returns
Column
one row per array item or map key value.
See also
pyspark.functions.posexplode()
pyspark.functions.explode_outer()
pyspark.functions.posexplode_outer()
Examples
>>> from pyspark.sql import Row >>> df = spark.createDataFrame([Row(a=1, intlist=[1,2,3], mapfield={"a": "b"})]) >>> df.select(explode(df.intlist).alias("anInt")).collect() [Row(anInt=1), Row(anInt=2), Row(anInt=3)]
>>> df.select(explode(df.mapfield).alias("key", "value")).show() +---+-----+ |key|value| +---+-----+ | a| b| +---+-----+