2个数据帧列之间的相似性

时间:2018-05-28 06:15:07

标签: python dataframe similarity sentence-similarity

我有两个数据帧,每个都有一个名为Song的列。但有时歌曲拼写不同。如何使用difflib(或类似的东西)在另一个数据帧的新列中获取一个数据帧的Song拼写?

例如:

Dataframe1

Song           Artist

like a virgi   madonna


Dataframe2

Song          Rank

like a virgin  2


Result

Song            Artist    SongAlt

like a virgin   Madonna   like a virgi

1 个答案:

答案 0 :(得分:1)

第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步:使用difflib的get_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