【发布时间】:2020-11-16 23:57:05
【问题描述】:
我有两个数据框 df1 和 df2 具有相同的列。我想找到这两个数据集之间的相似性。我一直在遵循这两种方法之一。 第一个是将两个数据框之一附加到另一个数据框并选择重复项:
df=pd.concat([df1,df2],join='inner')
mask = df.Check.duplicated(keep=False)
df[mask] # it gives me duplicated rows
第二个是考虑一个阈值,对于 df1 中的每一行,该阈值在 df2 中的行中找到一个潜在的匹配项。
数据样本:请注意数据集的长度不同
对于df1
Check
how to join to first row
large data work flows
I have two dataframes
fix grammatical or spelling errors
indent code by 4 spaces
why are you posting here?
add language identifier
my dad loves watching football
对于df2
Check
small data work flows
I have tried to puzze out an answer
mix grammatical or spelling errors
indent code by 2 spaces
indent code by 8 spaces
put returns between paragraphs
add curry on the chicken curry
mom!! mom!! mom!!
create code fences with backticks
are you crazy?
Trump did not win the last presidential election
为了做到这一点,我使用了以下函数:
def check(df1, thres, col):
matches = df1.apply(lambda row: ((fuzz.ratio(row['Check'], col) / 100.0) >= thres), axis=1)
return [df1. Check[i] for i, x in enumerate(matches) if x]
这应该可以让我找到匹配的行。
第二种方法(我最感兴趣的一种)的问题是它实际上没有考虑到两个数据帧。
我对第一个函数的期望值将是两个数据帧,一个用于 df1,一个用于 df2,有一个额外的列,其中包含与其他数据帧中的每行相比发现的相似性;从第二个代码中,我应该只为它们分配一个相似度值(我应该有与行数一样多的列)。
如果您需要更多信息或需要更多代码,请告诉我。 也许有一种更简单的方法可以确定这种相似性,但不幸的是我还没有找到。
欢迎提出任何建议。
预期输出:
(这是一个例子;因为我设置了一个阈值,所以输出可能会改变)
df1
Check sim
how to join to first row []
large data work flows [small data work flows]
I have two dataframes []
fix grammatical or spelling errors [mix grammatical or spelling errors]
indent code by 4 spaces [indent code by 2 spaces, indent code by 8 spaces]
why are you posting here? []
add language identifier []
my dad loves watching football []
df2
Check sim
small data work flows [large data work flows]
I have tried to puzze out an answer []
mix grammatical or spelling errors [fix grammatical or spelling errors]
indent code by 2 spaces [indent code by 4 spaces]
indent code by 8 spaces [indent code by 4 spaces]
put returns between paragraphs []
add curry on the chicken curry []
mom!! mom!! mom!! []
create code fences with backticks []
are you crazy? []
Trump did not win the last presidential election []
【问题讨论】:
-
我认为更实惠的方法是第二种方法,通过使用fuzzywuzzy 来确定相似度(我可以将其设置为1,但也可以将其设置为较低的值)。我的代码中的问题是它没有考虑两个数据集。我将 df1 中的所有行与 df2 中的所有行进行比较。当两行之间的相似度高于阈值时,我会添加到列表中(在 sim 列中,如预期的输出示例中所示)
-
请发布两个数据框的示例文本
-
请查看更新后的问题。非常感谢您的宝贵时间和帮助 anon01
标签: python pandas similarity fuzzywuzzy