メインコンテンツまでスキップ

配列を除く

col1 には存在するが col2 には存在しない要素を重複なしで含む新しい配列を返します。

構文

Python
from pyspark.sql import functions as sf

sf.array_except(col1, col2)

パラメーター

パラメーター

Type

説明

col1

pyspark.sql.Column または文字列

最初の配列を含む列の名前。

col2

pyspark.sql.Column または文字列

2 番目の配列を含む列の名前。

戻り値

pyspark.sql.Column: col1 に存在するが col2 には存在しない要素を含む新しい配列。

例1 :基本的な使い方

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]|
+--------------------+

例2 :共通要素がない場合

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]|
+--------------------------------------+

例3 :すべての共通要素を除く

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)|
+--------------------+
| []|
+--------------------+

例4 : null値を除く

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]|
+--------------------+

例5 : 空の配列を除く

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)|
+--------------------+
| []|
+--------------------+