開始使用免費開始

使用 outer join 篩選演員

使用 outer join 有個很實用的地方:因為它會回傳兩個表的所有列,且在不匹配處為 null,你可以用它找出在另一個表中沒有對應列的資料。來試試看吧!你拿到兩個表,分別列出兩部熱門電影的演員名單:Iron Man 1Iron Man 2。多數演員都有參與兩部片。請用 outer join 找出那些「沒有同時」參與兩部電影的演員。

Iron Man 1 的表叫做 iron_1_actorsIron Man 2 的表叫做 iron_2_actors。兩個表都已為你載入,並印出了幾列,讓你先了解其結構。

Venn graph with no overlap

本練習屬於課程

使用 pandas 進行資料表連接

檢視課程

練習說明

  • id 欄位進行 outer join,將 iron_1_actors(左)與 iron_2_actors 合併,將結果存成 iron_1_and_2,並把後綴設為 ('_1','_2')
  • 建立一個索引,當 name_1name_2 為 null 時回傳 True,否則回傳 False

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Merge iron_1_actors to iron_2_actors on id with outer join using suffixes
iron_1_and_2 = iron_1_actors.merge(____,
                                     ____,
                                     ____,
                                     suffixes=____)

# Create an index that returns true if name_1 or name_2 are null
m = ((iron_1_and_2['name_1'].____) | 
     (iron_1_and_2['____'].____))

# Print the first few rows of iron_1_and_2
print(iron_1_and_2[m].head())
編輯並執行程式碼