Anmerkung
Der Zugriff auf diese Seite erfordert eine Genehmigung. Du kannst versuchen, dich anzumelden oder die Verzeichnisse zu wechseln.
Der Zugriff auf diese Seite erfordert eine Genehmigung. Du kannst versuchen , die Verzeichnisse zu wechseln.
Gibt ein neues Array zurück, das die Vereinigung von Elementen in Col1 und Col2 ohne Duplikate enthält.
Syntax
from pyspark.sql import functions as sf
sf.array_union(col1, col2)
Die Parameter
| Parameter | Typ | Description |
|---|---|---|
col1 |
pyspark.sql.Column oder str |
Name der Spalte, die das erste Array enthält. |
col2 |
pyspark.sql.Column oder str |
Name der Spalte, die das zweite Array enthält. |
Rückkehr
pyspark.sql.Column: Ein neues Array, das die Vereinigung von Elementen in Col1 und Col2 enthält.
Examples
Beispiel 1: Grundlegende Verwendung
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["b", "a", "c"], c2=["c", "d", "a", "f"])])
df.select(sf.sort_array(sf.array_union(df.c1, df.c2))).show()
+-------------------------------------+
|sort_array(array_union(c1, c2), true)|
+-------------------------------------+
| [a, b, c, d, f]|
+-------------------------------------+
Beispiel 2: Union ohne gemeinsame Elemente
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["b", "a", "c"], c2=["d", "e", "f"])])
df.select(sf.sort_array(sf.array_union(df.c1, df.c2))).show()
+-------------------------------------+
|sort_array(array_union(c1, c2), true)|
+-------------------------------------+
| [a, b, c, d, e, f]|
+-------------------------------------+
Beispiel 3: Union mit allen gemeinsamen Elementen
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["a", "b", "c"], c2=["a", "b", "c"])])
df.select(sf.sort_array(sf.array_union(df.c1, df.c2))).show()
+-------------------------------------+
|sort_array(array_union(c1, c2), true)|
+-------------------------------------+
| [a, b, c]|
+-------------------------------------+
Beispiel 4: Union mit Nullwerten
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["a", "b", None], c2=["a", None, "c"])])
df.select(sf.sort_array(sf.array_union(df.c1, df.c2))).show()
+-------------------------------------+
|sort_array(array_union(c1, c2), true)|
+-------------------------------------+
| [NULL, a, b, c]|
+-------------------------------------+
Beispiel 5: Union mit leeren Arrays
from pyspark.sql import Row, functions as sf
from pyspark.sql.types import ArrayType, StringType, StructField, StructType
data = [Row(c1=[], c2=["a", "b", "c"])]
schema = StructType([
StructField("c1", ArrayType(StringType()), True),
StructField("c2", ArrayType(StringType()), True)
])
df = spark.createDataFrame(data, schema)
df.select(sf.sort_array(sf.array_union(df.c1, df.c2))).show()
+-------------------------------------+
|sort_array(array_union(c1, c2), true)|
+-------------------------------------+
| [a, b, c]|
+-------------------------------------+