【发布时间】:2017-05-16 01:21:12
【问题描述】:
目标
- 给定一个 excel 文件(全是错别字),使用 FuzzyWuzzy 比较和
将拼写错误与
accepted列表相匹配。 - 用最接近的
accepted匹配更正填错的 Excel 文件。
方法
- 使用 pandas 导入 Excel 文件
- 将原始的、拼写错误的 Excel 文件推送到数据框中
- 创建一个
accepted数据框 - 使用FuzzyWuzzy 比较错字数据框与
accepted数据框 - 返回原始拼写、接受的拼写和匹配分数
- 将相关的、可接受的拼写附加到所有拼写的原始 Excel 文件/行中
代码
#Load Excel File into dataframe
xl = pd.read_excel(open("/../data/expenses.xlsx",'rb'))
#Let's clarify how many similar categories exist...
q = """
SELECT DISTINCT Expense
FROM xl
ORDER BY Expense ASC
"""
expenses = sqldf(q)
print(expenses)
#Let's add some acceptable categories and use fuzzywuzzy to match
accepted = ['Severance', 'Legal Fees', 'Import & Export Fees', 'I.T. Fees', 'Board Fees', 'Acquisition Fees']
#select from the list of accepted values and return the closest match
process.extractOne("Company Acquired",accepted,scorer=fuzz.token_set_ratio)
('购置费', 38) 分数不高,但足够高,可以返回预期的输出
!!!!!问题!!!!!
#Time to loop through all the expenses and use FuzzyWuzzy to generate and return the closest matches.
def correct_expense(expense):
for expense in expenses:
return expense, process.extractOne(expense,accepted,scorer = fuzz.token_set_ratio)
correct_expense(expenses)
('费用', ('法律费用', 47))
问题
- 如您所见,process.extractOne 在逐个测试时运行正确。但是,在循环中运行时,返回值是意外的。我相信我可能会抓住第一列或最后一列,但即使是这样,我也希望“董事费”或“收购”会弹出(参见原始 excel 文件)。
【问题讨论】:
-
correct_expense()中至少有两个问题:你在循环内部返回,参数名称与循环变量相同。
标签: python pandas fuzzywuzzy