Aller au contenu principal

array_except

Renvoie un nouveau tableau contenant les éléments présents dans col1 mais pas dans col2, sans doublons.

Syntaxe

Python
from pyspark.sql import functions as sf

sf.array_except(col1, col2)

parameter

parameter

Type

Description

col1

pyspark.sql.Column ou str

Nom de la colonne contenant le premier tableau.

col2

pyspark.sql.Column ou str

Nom de la colonne contenant le deuxième tableau.

parameter

Type

Description

col1

pyspark.sql.Column ou str

Nom de la colonne contenant le premier tableau.

col2

pyspark.sql.Column ou str

Nom de la colonne contenant le deuxième tableau.

Renvoie

pyspark.sql.Column: Un nouveau tableau contenant les éléments présents dans col1 mais pas dans col2.

Exemples

Exemple 1 : utilisation de base

Python
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["b", "a", "c"], c2=["c", "d", "a", "f"])])
df.select(sf.array_except(df.c1, df.c2)).show()
Output
+--------------------+
|array_except(c1, c2)|
+--------------------+
| [b]|
+--------------------+

Exemple 2 : sauf sans éléments communs

Python
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_except(df.c1, df.c2))).show()
Output
+--------------------------------------+
|sort_array(array_except(c1, c2), true)|
+--------------------------------------+
| [a, b, c]|
+--------------------------------------+

Exemple 3 : Sauf avec tous les éléments communs.

Python
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["a", "b", "c"], c2=["a", "b", "c"])])
df.select(sf.array_except(df.c1, df.c2)).show()
Output
+--------------------+
|array_except(c1, c2)|
+--------------------+
| []|
+--------------------+

Exemple 4 : Sauf avec les valeurs nulles

Python
from pyspark.sql import Row, functions as sf
df = spark.createDataFrame([Row(c1=["a", "b", None], c2=["a", None, "c"])])
df.select(sf.array_except(df.c1, df.c2)).show()
Output
+--------------------+
|array_except(c1, c2)|
+--------------------+
| [b]|
+--------------------+

Exemple 5 : sauf avec les tableaux vides

Python
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.array_except(df.c1, df.c2)).show()
Output
+--------------------+
|array_except(c1, c2)|
+--------------------+
| []|
+--------------------+