Piezīmes
Lai piekļūtu šai lapai, ir nepieciešama autorizācija. Varat mēģināt pierakstīties vai mainīt direktorijus.
Lai piekļūtu šai lapai, ir nepieciešama autorizācija. Varat mēģināt mainīt direktorijus.
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.
Syntax
from pyspark.sql import functions as sf
sf.posexplode(col)
Parameters
| Parameter | Type | Description |
|---|---|---|
col |
pyspark.sql.Column or column name |
Target column to work on. |
Returns
pyspark.sql.Column: one row per array item or map key value including positions as a separate column.
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 |
+---+---------------------------+---+---+-----+