如何使用 pyspark 2.1.0 select 另一个数据框中不存在的行?
How to select rows that are not present in another dataframe ith pyspark 2.1.0?
环境
- pyspark 2.1.0
上下文
我有两个具有以下结构的数据框:
数据帧 1:
id | ... | distance
数据帧 2:
id | ... | distance | other calculated values
第二个数据帧是基于数据帧 1 的过滤器创建的。此过滤器 selects,来自数据帧 1,只有距离 <= 30.0。
请注意,dataframe1 将在多行中包含相同的 ID。
问题
我需要从数据框 1 的行中 select 使用数据框 2 中没有出现的 ID。
目的是selectID没有距离小于或等于30.0的行。
测试解决方案
我已经尝试过 leftanti 连接,根据不是官方文档而是互联网上的来源(因为,嘿,他们为什么要解释它?):select all rows from df1 that are not present in df2
distinct_id_thirty = within_thirty_km \
.select("id") \
.distinct()
not_within_thirty_km = data_with_straight_distance.join(
distinct_id_thirty,
"id",
"leftanti")
其中:
within_thrity_km
是过滤器 filter(col("distance") <= 30.0)
在 data_with_straight_distance
上产生的数据帧
data_with_straight_distance
是一个包含所有数据的数据框。
distinct_id_thirty
是一个数据框,包含来自数据框 within_thirty_km
的不同 ID 列表
问题
上述 returns 距离低于 30 的数据。所以我假设我做错了什么:
- 我做错了什么?
- 这是解决这个问题的好方法吗?如果没有,我应该如何进行?
编辑:
这是我期望的最小示例:
data = [
("1", 15),
("1", 35),
("2", 15),
("2", 30),
("3", 35)]
data = spark.createDataFrame(data, ['id', 'distance'])
data.show()
thirty = data.filter(col("distance") <= 30)
dist_thirty = thirty.select("id").distinct()
not_in_thirty = data.join(dist_thirty, "id", "left_anti")
print("thirty")
thirty.show()
print("distinst thirty")
dist_thirty.show()
print("not_in_thirty")
not_in_thirty.show()
输出:
+---+--------+
| id|distance|
+---+--------+
| 3| 35|
+---+--------+
但我的实际数据确实得到距离 <= 30,其中 运行。
"leftanti"
应按照以下文档替换为 "left_anti"
:
https://spark.apache.org/docs/2.4.0/api/python/pyspark.sql.html#pyspark.sql.DataFrame.join
环境
- pyspark 2.1.0
上下文
我有两个具有以下结构的数据框:
数据帧 1:
id | ... | distance
数据帧 2:
id | ... | distance | other calculated values
第二个数据帧是基于数据帧 1 的过滤器创建的。此过滤器 selects,来自数据帧 1,只有距离 <= 30.0。
请注意,dataframe1 将在多行中包含相同的 ID。
问题
我需要从数据框 1 的行中 select 使用数据框 2 中没有出现的 ID。
目的是selectID没有距离小于或等于30.0的行。
测试解决方案
我已经尝试过 leftanti 连接,根据不是官方文档而是互联网上的来源(因为,嘿,他们为什么要解释它?):select all rows from df1 that are not present in df2
distinct_id_thirty = within_thirty_km \
.select("id") \
.distinct()
not_within_thirty_km = data_with_straight_distance.join(
distinct_id_thirty,
"id",
"leftanti")
其中:
within_thrity_km
是过滤器filter(col("distance") <= 30.0)
在data_with_straight_distance
上产生的数据帧
data_with_straight_distance
是一个包含所有数据的数据框。distinct_id_thirty
是一个数据框,包含来自数据框within_thirty_km
的不同 ID 列表
问题
上述 returns 距离低于 30 的数据。所以我假设我做错了什么:
- 我做错了什么?
- 这是解决这个问题的好方法吗?如果没有,我应该如何进行?
编辑:
这是我期望的最小示例:
data = [
("1", 15),
("1", 35),
("2", 15),
("2", 30),
("3", 35)]
data = spark.createDataFrame(data, ['id', 'distance'])
data.show()
thirty = data.filter(col("distance") <= 30)
dist_thirty = thirty.select("id").distinct()
not_in_thirty = data.join(dist_thirty, "id", "left_anti")
print("thirty")
thirty.show()
print("distinst thirty")
dist_thirty.show()
print("not_in_thirty")
not_in_thirty.show()
输出:
+---+--------+
| id|distance|
+---+--------+
| 3| 35|
+---+--------+
但我的实际数据确实得到距离 <= 30,其中 运行。
"leftanti"
应按照以下文档替换为 "left_anti"
:
https://spark.apache.org/docs/2.4.0/api/python/pyspark.sql.html#pyspark.sql.DataFrame.join