當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PySpark DataFrame intersectAll方法用法及代碼示例

PySpark DataFrame 的 intersectAll(~) 方法返回一個新的 PySpark DataFrame,其中的行也存在於其他 PySpark DataFrame 中。與 intersect(~) 不同,intersectAll(~) 方法保留重複項。

注意

intersectAll(~) 方法與 SQL 中的 INTERSECT ALL 語句相同。

參數

1.other | PySpark 數據幀

另一個PySpark 數據幀。

返回值

一個新的 PySpark 數據幀。

例子

考慮以下PySpark DataFrame:

df = spark.createDataFrame([("Alex", 20), ("Alex", 20), ("Bob", 30), ("Cathy", 40)], ["name", "age"])
df.show()



+-----+---+
| name|age|
+-----+---+
| Alex| 20|
| Alex| 20|
|  Bob| 30|
|Cathy| 40|
+-----+---+

假設另一個PySpark DataFrame 是:

df_other = spark.createDataFrame([("Alex", 20), ("Alex", 20), ("David", 80), ("Eric", 80)], ["name", "age"])
df_other.show()



+-----+---+
| name|age|
+-----+---+
| Alex| 20|
| Alex| 20|
|David| 80|
| Eric| 80|
+-----+---+

在此,請注意以下事項:

  • 唯一匹配的行是 Alex 的行

  • Alex 的行在 dfdf_other 中出現兩次

獲取其他 PySpark DataFrame 中也存在的行,同時保留重複項

要獲取其他 PySpark DataFrame 中也存在的行,同時保留重複項:

df_res = df.intersectAll(df_other)
df_res.show()



+----+---+
|name|age|
+----+---+
|Alex| 20|
|Alex| 20|
+----+---+

請注意以下事項:

  • Alex 的行重複,因為 Alex 的行在 dfdf_other 中分別出現兩次。

  • 如果 Alex 的行僅在一個 DataFrame 中出現一次,但在另一個 DataFrame 中出現多次,則 Alex 的行將僅在生成的 DataFrame 中包含一次。

  • 如果您隻想包含一次重複行,請改用 intersect(~) 方法。

相關用法


注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 PySpark DataFrame | intersectAll method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。