【问题标题】:How to replace misspelled words in a pandas dataframe如何替换熊猫数据框中拼写错误的单词
【发布时间】:2019-10-22 14:40:58
【问题描述】:

我有 2 个熊猫数据框。一个包含正确拼写的单词列表:

[In]: df1
[Out]:
   words
0  apple
1  phone
2  clock
3  table
4  clean

还有一个拼写错误的单词:

[In]: df2
[Out]:
   misspelled
0        aple
1         phn
2        alok
3     garbage
4        appl
5         pho

目标是使用第一个 DataFrame 中拼写正确的单词列表替换第二个 DataFrame 中拼写错误的单词列。第二个 DataFrame 可以有多次重复,可以是与第一个不同的大小,可以包含不在第一个 DataFrame 中的单词(或不够相似而无法匹配)。

我一直在尝试使用 difflib.get_close_matches 并取得了一些成功,但效果并不理想。

这是我目前所拥有的:

x = list(map(lambda x: get_close_matches(x, df1.col1), df2.col1))
good_words = list(map(''.join, x))
l = np.array(good_words, dtype='object')
df2.col1 = pd.Series(l)
df2 = df2[df2.col1 != '']

应用转换后,我应该得到第二个 DataFrame 的样子:

[In]: df2
[Out]:
          0
0     apple
1     phone
2     clock
3       NaN
4     apple
5     phone

如果未找到匹配项,则该行将替换为 NaN。我的问题是我得到的结果如下所示:

[In]: df2
[Out]:
    misspelled
0        apple
1        phone
2   clockclean
3          NaN
4        apple
5        phone

在写这篇文章的时候,我还没有弄清楚为什么有些词是组合在一起的。我怀疑这与difflib.get_close_matches 匹配长度和/或字母相似的不同单词有关。到目前为止,我在整个专栏中得到了大约 10% - 15% 的单词像这样组合。 提前致谢。

【问题讨论】:

  • 可能是dupe
  • 并非如此。尽管我承认两者的结果相似,但有两个主要区别。首先在您的question 中处理实际的拼写检查和替换。在这个问题中,我使用get_close_matches() 进行拼写检查。我遇到的主要问题实际上是替换单词。其次,我使用的是 pandas DataFrame,而不是字典。

标签: python python-3.x pandas numpy dataframe


【解决方案1】:

如果想要匹配get_close_matches 返回的第一个值,可以根据您所需的阈值调整截止参数,如果不匹配,请使用nextiter 以获得可能的附加值 - 这里np.nan

x = [next(iter(x), np.nan) 
          for x in map(lambda x: difflib.get_close_matches(x, df1.words, cutoff = 0.6), df2.misspelled)]
df2['col1'] = x

print (df2)
  misspelled   col1
0       aple  apple
1        phn  phone
2       alok  clock
3    garbage    NaN
4       appl  apple
5        pho  phone

【讨论】:

    【解决方案2】:

    另一种方法是使用pandas-dedupe
    由于您有一个混乱的数据集和一个规范的数据集(即公报),您可以执行地名词典重复数据删除。

    pandas-dedupe 功能特别强大,因为它将主动学习与逻辑回归和聚类相结合。它使用户可以很好地控制如何执行重复数据删除,同时将繁重的工作降至最低。

    示例代码

    import pandas as pd
    import pandas dedupe
    
    import pandas as pd
    import pandas_dedupe
    
    clean_data = pd.DataFrame({'name': ['apple', 'phone', 'clock', 'table', 'clean']})
    messy_data = pd.DataFrame({'name':['aple', 'phn', 'alok', 'garbage', 'appl', 'apple', 'clock', 'phone', 'phone']})
    
    
    dd = pandas_dedupe.gazetteer_dataframe(
        clean_data, 
        messy_data, 
        field_properties = 'name', 
        canonicalize=True,
        )
    
    # At this point, pandas-dedupe will ask you to label a few examples as duplicates or distinct.   
    # Once done, you hit finish and the output will look like this:   
    
    # name      cluster id  confidence  canonical_name
    # 0 aple    0.0          0.636356   apple
    # 1 phn     1.0          0.712090   phone
    # 2 alok    2.0          0.492138   clock
    # 3 garbage NaN          NaN        NaN
    # 4 appl    0.0          0.906788   apple
    # 5 apple   0.0          0.921466   apple
    # 6 clock   2.0          0.921466   clock
    # 7 phone   1.0          0.921466   phone
    # 8 phone   1.0          0.921466   phone
    

    我知道这个问题很老了,但我希望这个例子将来对某人有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多