2017-04-17 43 views
0

我可能会用错误的方法解决这个问题。我已经将两个带有公司名称的csv文档加载到列表中,并且试图比较两个列表以找到名称相似的名称。使用Python和Pandas将对象列表与另一个列表中的对象进行比较

这些名称被降低了下标和删除了标点符号,但有时输入信息的人有时会缩短公司标识或拼写错误,因此我试图根据名称的相似性来找到一种分数分配方法。

原始数据可能看起来像这样(没有实际数据):

Walgreens Boots Alliance 
CARDINAL HEALTH 
EXPRESS SCRIPTS HOLDING 
j.p. morgan chase 
Bank of America Corp 
wells fargo 
Home Depot 
STATE FARM INSURANCE COS. 
Johnson & Johnson 
archer daniels midland 

然后在下壳,去掉停用词/标点符号和分裂:

[walgreens, boots, alliance] 
[cardinal, health] 
[express, scripts, holding] 
[jp, morgan, chase] 
[bank, america, corp] 
[wells, fargo] 
[home, depot] 
[state, farm, insurance, cos] 
[johnson, johnson] 
[archer, daniels, midland] 

...和类似的第二列表看起来是这样的:

[cardinal, health] 
[expres, scripts, holding] 
[bank, america, corporation] 
[wells, fargo] 
[home, depot] 
[state, farm, insurance, companies] 
[archer, daniels] 
[ford, motor, company] 
[general, motors] 
[john, deere] 

我已经写了一个令人费解的循环熊猫s到测试,如果列表中的每个单词也存在于其他列表:

for index, row in df1[['Company Name Tokens']].iterrows(): 
    for content in row: 
     for x in content: 
      df1.iloc[index]['Test'] = 0 
      df1.iloc[index]['Count'] = len(content) 
      for idx, rw in entities[['Company Name Tokens']]: 
       for r in rw: 
        if x in r: 
         df1.iloc[index]['Test'] = df1.iloc[index]['Test'] + 1 

我意识到这可能是很慢的,但我没有效率了。无论如何,我认为,这种做法可能是太多了Python解释器来处理,因为我得到了一个错误:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-192-adea96d8cb82> in <module>() 
     4    df1.iloc[index]['Test'] = 0 
     5    df1.iloc[index]['Count'] = len(content) 
----> 6    for idx, rw in entities[['Company Name Tokens']]: 
     7     for r in rw: 
     8      if x in r: 

ValueError: too many values to unpack (expected 2) 

我是不是使这个太困难,有没有更好的方式来做到这一点?

+0

这种“天真”的做法总是很慢。你想使用一些更好的数据结构,如倒排索引 - 根据你的最终任务创建一个包含行号列表的单词字典,或者类似的东西。 – liborm

+0

@liborm很有趣......所以像{'archer':[1,2,3,4],'daniels':[1,2,3,4],'midland':[]}? – Twitch

+0

您需要更多地指定您的任务以获得至少一个数据结构或算法的建议。但通常情况下是 - 将其中一个数据集处理到索引中,并逐个扫描其他数据集该指数。 – liborm

回答

0

如果你只是有两个列表来开始你可以得到的集合的并集为每个列表:

shared = set(list_a).union(set(list_b)) 

你只需要两个列表是1-d代替2-d为DataFrames是。当然,这只有在数据以标准方式输入时才有效。正如你所提到的,你必须清理两个输入错误。

相关问题