Showing place where is the difference between two strings in two dataframe columns, pandas

Viewed 1183

I am looking for solution to show place in strings where are difference between two columns

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'

right now i have only way to search differences(but doesnt work , dont know why )

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)
1 Answers

Your question is quite untrivial and as mentioned in the comments, it's probably best to use the difflib.Sequencematcher.get_matching_blocks for this, but I couldn get it to work. So here's a working solution, which will not perform in terms of speed, but get's the output.

First we get the difference in words, then we find the starting + ending position in each column:

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)

Output

                          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]
Related