第 1 步:合并任何可以合并的内容
In [67]: df1
Out[67]:
Song Artist
0 mysong myartist
1 like a virgi madonna
In [68]: df2
Out[68]:
Song Rank
0 mysong 1
1 like a virgin 2
In [69]: merged = pd.merge(df1, df2, on='Song')
In [70]: merged
Out[70]:
Song Artist Rank
0 mysong myartist 1
第 2 步:找出剩余的内容
In [71]: unmerged = df2[~df2.isin(merged)].dropna()
In [72]: unmerged
Out[72]:
Song Rank
1 like a virgin 2.0
第 3 步:使用 difflibget_close_matches
获取最接近的匹配项
In [73]: songs = list(df1['Song'].unique())
In [74]: def closest(a):
...: try:
...: return difflib.get_close_matches(a, songs)[0]
...: except IndexError:
...: return "Not Found"
In [75]: unmerged['closest_song'] = unmerged.apply(lambda row: closest(row['Song']), axis=1)
In [76]: unmerged
Out[76]:
Song Rank closest_song
1 like a virgin 2.0 like a virgi
第 4 步:根据需要获取相似度百分比
In [77]: def similar(a, b):
...: return difflib.SequenceMatcher(None, a, b).ratio()
In [78]: unmerged['Similarity'] = unmerged.apply(lambda row: similar(row['closest_song'], row['Song']), axis=1)
In [79]: unmerged
Out[79]:
Song Rank closest_song Similarity
1 like a virgin 2.0 like a virgi 0.96
第 5 步:使用最接近的值进行合并
In [80]: unmerged.rename(columns={'Song': 'Old_Song', 'closest_song': 'Song'}, inplace=True)
In [81]: new = unmerged.merge(df1, on='Song')[['Song', 'Artist', 'Rank']]
Out[81]:
Song Artist Rank
0 like a virgi madonna 2.0
In [82]: pd.concat([merged, new])
Out[82]:
Song Artist Rank
0 mysong myartist 1.0
0 like a virgi madonna 2.0