在两个 Pandas 数据帧的两列中,显示两个字符串之间差异的位置。

5

我正在寻找一种解决方案,以显示两个列之间差异的位置。

Input:
df=pd.DataFrame({'A':['this is my favourite one','my dog is the best'],
                 'B':['now is my favourite one','my doggy is the worst']})

expected output:
[A-B],[B-A]
0:4 ,0:3      #'this','now'
3:6 ,3:8      #'dog','doggy'
14:18,16:21   #'best','worst'

目前我只有一种方法来搜索差异(但不起作用,不知道为什么)

df['A-B']=df.apply(lambda x: x['A'].replace(x['B'], "").strip(),axis=1)
df['B-A']=df.apply(lambda x: x['B'].replace(x['A'], "").strip(),axis=1)

尝试使用difflib: http://docs.python.org/library/difflib - nosklo
嗨,difflib不能像我需要的那样显示字符串中的位置,只能用“^”来标记,或者我错了吗?我只需要像我的预期输出那样。 - sygneto
它具有返回位置的功能。 - nosklo
字符串总是有相同数量的单词吗? - Florian Bernard
弗洛里安,这些字符串的大小总是不同的。 - sygneto
你实际上想要整个单词,而不仅仅是差异之处。 - Dani Mesejo
1个回答

3
你的问题并不简单,正如评论中提到的那样,最好使用 difflib.Sequencematcher.get_matching_blocks,但我没能使其正常工作。因此,这里提供一种有效的解决方案,尽管速度可能较慢,但可以得到输出结果。
首先,我们获取各列单词之间的差异,然后找到每列的起始和结束位置:
def get_diff_words(col1, col2):
    diff_words = [[w1, w2] for w1, w2 in zip(col1, col2) if w1 != w2]

    return diff_words

df['diff_words'] = df.apply(lambda x: get_diff_words(x['A'].split(), x['B'].split()), axis=1)
df['pos_A'] = df.apply(lambda x: [f'{x["A"].find(word[0])}:{x["A"].find(word[0])+len(word[0])}' for word in x['diff_words']], axis=1)
df['pos_B'] = df.apply(lambda x: [f'{x["B"].find(word[1])}:{x["B"].find(word[1])+len(word[1])}' for word in x['diff_words']], axis=1)

输出

                          A                        B                     diff_words         pos_A         pos_B
0  this is my favourite one  now is my favourite one                  [[this, now]]         [0:4]         [0:3]
1        my dog is the best    my doggy is the worst  [[dog, doggy], [best, worst]]  [3:6, 14:18]  [3:8, 16:21]

这假设句子始终有相同数量的单词。 - Dani Mesejo
我假设是这样的,否则你怎么能首先比较位置呢?那么比较位置的整个原因是什么呢? - Erfan
@Erfan,我正在使用Python 2.7,有更兼容的版本吗?有时这些句子长度不同,我的方法是从两个句子中删除共同部分,然后检查其余部分与原始字符串的位置。 - sygneto
我建议您提出一个新问题,并使用更好的示例数据框,以更好地展示您的实际数据集。如果您在此处链接问题,我将再次尝试回答它。我认为这个问题已经得到了解答,因为它根据您的示例数据生成了正确的输出。此外,您是否知道python 2.7将于今年年底被弃用。@sygneto - Erfan
@Erfan 我知道有 Python 2.7 版本,但是我正在使用只能在旧版本 Python 上运行的库,所以我才使用它。 - sygneto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接