gpt4 book ai didi

python - 如何计算两列之间的模糊比?

转载 作者:行者123 更新时间:2023-11-30 21:53:32 25 4
gpt4 key购买 nike

开始使用 Pandas。

I have two columns:
A B
Something Something Else
Everything Evythn
Someone Cat
Everyone Evr1

我想计算两列之间每一行的模糊比,因此输出将如下所示:

A                     B                  Ratio
Something Something Else 12
Everything Evythn 14
Someone Cat 10
Everyone Evr1 20

我怎样才能实现这个目标?两列都在同一个 df 中。

最佳答案

将 lambda 函数与 DataFrame.apply 结合使用:

from fuzzywuzzy import fuzz

df['Ratio'] = df.apply(lambda x: fuzz.ratio(x.A, x.B), axis=1)
#alternative with list comprehension
#df['Ratio'] = [fuzz.ratio(a, b) for a,b in zip(df.A, df.B)]
print (df)
A B Ratio
0 Something Something Else 78
1 Everything Evythn 75
2 Someone Cat 0
3 Everyone Evr1 50

编辑:

如果可能,列中缺少一些值,则失败,因此添加 DataFrame.dropna :

print (df)
A B
0 Something Something Else
1 Everything NaN
2 Someone Cat
3 Everyone Evr1

from fuzzywuzzy import fuzz

df['Ratio'] = df.dropna(subset=['A', 'B']).apply(lambda x: fuzz.ratio(x.A, x.B), axis=1)
print (df)
A B Ratio
0 Something Something Else 78.0
1 Everything NaN NaN
2 Someone Cat 0.0
3 Everyone Evr1 50.0

关于python - 如何计算两列之间的模糊比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com