EXISTS サブクエリの Column オブジェクトを返します。
構文
exists()
返品
Column: EXISTS サブクエリを表す Column オブジェクト。
メモ
exists メソッドは、サブクエリに関連するレコードが存在することを確認するブール型列を作成する方法を提供します。
DataFrame内で適用すると、このメソッドを使用すると、一致するレコードが関連するデータセットに存在するかどうかに基づいて行をフィルター処理できます。 結果の Column オブジェクトは、フィルター条件で直接、または計算列として使用できます。
例示
data_customers = [
(101, "Alice", "USA"), (102, "Bob", "Canada"), (103, "Charlie", "USA"),
(104, "David", "Australia")
]
data_orders = [
(1, 101, "2023-01-15", 250), (2, 102, "2023-01-20", 300),
(3, 103, "2023-01-25", 400), (4, 101, "2023-02-05", 150)
]
customers = spark.createDataFrame(
data_customers, ["customer_id", "customer_name", "country"])
orders = spark.createDataFrame(
data_orders, ["order_id", "customer_id", "order_date", "total_amount"])
from pyspark.sql import functions as sf
customers.alias("c").where(
orders.alias("o").where(
sf.col("o.customer_id") == sf.col("c.customer_id").outer()
).exists()
).orderBy("customer_id").show()
# +-----------+-------------+-------+
# |customer_id|customer_name|country|
# +-----------+-------------+-------+
# | 101| Alice| USA|
# | 102| Bob| Canada|
# | 103| Charlie| USA|
# +-----------+-------------+-------+
customers.alias("c").where(
~orders.alias("o").where(
sf.col("o.customer_id") == sf.col("c.customer_id").outer()
).exists()
).orderBy("customer_id").show()
# +-----------+-------------+---------+
# |customer_id|customer_name| country|
# +-----------+-------------+---------+
# | 104| David|Australia|
# +-----------+-------------+---------+