把它们链接起来!
在上一课中,您已完成将 restaurants 与 restaurants_new 进行链接的大部分工作。您生成了可能匹配行的不同配对,在 cuisine_type 和 city 列上查找了精确匹配,并在 rest_name 列上比较了相似字符串。包含分数的 DataFrame 已存入 potential_matches。
现在终于要把这两个 DataFrame 链接起来了。首先,您将从 potential_matches 中提取在上述列上匹配的 restaurants_new 的所有行索引。然后,基于这些索引对 restaurants_new 做子集筛选,最后把非重复的取值与 restaurants 进行拼接。所有 DataFrame 已在您的环境中,pandas 已以 pd 导入。
本练习是课程的一部分
Python 数据清洗
练习说明
- 使用
.sum()方法筛选potential_matches中按行求和大于等于 3 的行。 - 使用
.get_level_values()从matches中提取第二级列索引,它代表来自restaurants_new的匹配记录的行索引。 - 对
restaurants_new进行子集筛选,保留下标不在matching_indices中的行。 - 将
restaurants与non_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)