開始使用免費開始

把它們連起來!

在上一個單元中,你已經完成將 restaurantsrestaurants_new 連結的大部分工作。你已經產生了可能相符列的各種配對,對 cuisine_typecity 欄位進行了完全比對,並對 rest_name 欄位進行相似字串比對。你把包含分數的 DataFrame 存成 potential_matches

現在終於要把兩個 DataFrame 連結起來了。你會先從 potential_matches 中擷取在上述欄位都能對上的 restaurants_new 列索引。接著以這些索引在 restaurants_new 中進行子集篩選,最後把非重複的值與 restaurants 串接起來。所有的 DataFrame 都已在你的環境中,且已將 pandaspd 匯入。

本練習屬於課程

用 Python 進行資料清理

檢視課程

練習說明

  • 使用 .sum(),篩出 potential_matches 中列加總大於等於 3 的列。
  • 使用 .get_level_values()matches 取出第二個欄索引,該索引代表 restaurants_new 中相符紀錄的列索引。
  • 針對不在 matching_indices 中的索引,從 restaurants_new 取出子集。
  • restaurantsnon_dup 串接起來。

動手互動練習

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

# Isolate potential matches with row sum >=3
matches = ____[____.___(____) >= ____]

# Get values of second column index of matches
matching_indices = matches.____.____(____)

# Subset restaurants_new based on non-duplicate values
non_dup = ____[~restaurants_new.index.____(____)]

# Concatenate restaurants and non_dup
full_restaurants = pd.____([____, ____])
print(full_restaurants)
編輯並執行程式碼