我正在构建一个模糊搜索程序,使用 FuzzyWuzzy 在数据集中查找匹配的名称。正如预期的那样,我的数据位于大约 10378 行len(df['Full name'])
的 DataFrame 中,为 10378。但是len(choices)
只有1695。
我在 IPython Notebook 中运行 Python2.7.10
和 pandas 。0.17.0
choices = df['Full name'].astype(str).to_dict()
def fuzzy_search_to_df (term, choices=choices):
search = process.extract(term, choices, limit=len(choices)) # does the search itself
rslts = pd.DataFrame(data=search, index=None, columns=['name', 'rel', 'df_ind']) # puts the results in DataFrame form
return rslts
results = fuzzy_search_to_df(term='Ben Franklin') # returns the search result for the given term
matches = results[results.rel > 85] # subset of results, these are the best search results
find = df.iloc[matches['df_ind']] # matches in the main df
正如您可能知道的choices
那样,我在 dict 中获得了结果的索引df_ind
,我认为它与主数据框中的索引相同。
我相当确定问题出在to_dict()
函数的第一行,len(df['Full name'].astype(str)
结果为 10378,len(df['Full name'].to_dict())
结果为 1695。