使用fuzzywuzzy在数据框中创建一列匹配结果

时间:2016-06-17 22:16:08

标签: python pandas fuzzywuzzy

我在使用FuzzyWuzzy库将所有结果存储到数据框列中时遇到了挑战(我猜测它可能需要循环?)我一直在挠头在这一整天,现在我想看看你们中是否有人可以帮我解决这个问题!会非常有帮助!

作为我尝试做的一个例子,这里有2个数据框表......

主表

+----+-----------------+
| ID |      ITEM       |
+----+-----------------+
|    |                 |
| 1  | Pepperoni Pizza |
|    |                 |
| 2  | Cheese Pizza    |
|    |                 |
| 3  | Chicken Salad   |
|    |                 |
| 4  | Plain Salad     |
+----+-----------------+

查找表

+--------------+---+
| LOOKUP VALUE | - |
+--------------+---+
|              |   |
| Cheese       | - |
|              |   |
| Salad        | - |
+--------------+---+

本质上,我试图将查找表的值用于Master表中的整个值列表,并将结果存储在第三个表中。

以下是我希望最终输出看起来......

+--------------+----------------------------+-------------------+
| LOOKUP VALUE |       MATCHED VALUES       | MATCHED VALUE IDS |
+--------------+----------------------------+-------------------+
|              |                            |                   |
| Cheese       | Cheese Pizza               | 2                 |
|              |                            |                   |
| Salad        | Chicken Salad, Plain Salad | 3,4               |
+--------------+----------------------------+-------------------+

我知道模糊Wuzzy的基础知识,这里是我的开始:

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

choices = ["Pepperoni Pizza","Cheese Pizza","Chicken Salad", "Plain Salad"]
process.extract("salad",choices,limit=2)

输出 = [(' Chicken Salad',90),(' Plain Salad',90)]

很好,但是如何系统地执行此操作,针对主表中的所有值运行所有查找值?

非常感谢您阅读我!

1 个答案:

答案 0 :(得分:3)

在DataFrame中存储列表不是一个好主意,我建议将每个匹配存储在DataFrame中。这是代码:

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

import pandas as pd
import io

master = pd.read_csv(io.StringIO("""ID,ITEM
1,Pepperoni Pizza
2,Cheese Pizza
3,Chicken Salad
4,Plain Salad"""))

lookups = ["Cheese", "Salad"]

choices = master.set_index("ID").ITEM.to_dict()

res = [(lookup,) + item for lookup in lookups for item in process.extract(lookup, choices,limit=2)]
df = pd.DataFrame(res, columns=["lookup", "matched", "score", "id"])
df

输出:

   lookup        matched  score  id
0  Cheese   Cheese Pizza     90   2
1  Cheese  Chicken Salad     45   3
2   Salad  Chicken Salad     90   3
3   Salad    Plain Salad     90   4

基本上,我从choices创建一个master dict进行匹配,然后循环lookups并将结果存储为列表。并最终将列表转换为DataFrame。