Column返回 EXISTS 子查询的对象。
Syntax
exists()
退货
Column:表示 Column EXISTS 子查询的对象。
备注
该方法 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|
# +-----------+-------------+---------+