我试图辨别两个字符串之间的字符串相似性(使用Jaro)。每个字符串都位于我的数据框中的单独列中。
String 1 = df['name_one']
String 2 = df['name_two']
当我尝试运行字符串相似性逻辑时:
from pyjarowinkler import distance
df['distance'] = df.apply(lambda d: distance.get_jaro_distance(str(d['name_one']),str(d['name_two']),winkler=True,scaling=0.1), axis=1)
我收到以下错误:
**error: JaroDistanceException: Cannot calculate distance from NoneType (str, str)**
太好了,所以列中没有任何类型,所以我要做的第一件事就是检查:
maskone = df['name_one'] == None
df[maskone]
masktwo = df['name_two'] == None
df[masktwo]
这将导致未找到None类型。...我现在在这里挠头,但是继续以任何方式清理两列。
df['name_one'] = df['name_one'].fillna('').astype(str)
df['name_two'] = df['name_two'].fillna('').astype(str)
但是,我仍然得到:
error: JaroDistanceException: Cannot calculate distance from NoneType (str, str)
我可以正确删除NoneTypes吗?
答案 0 :(得分:1)
问题并不完全是您只遇到NoneTypes
,而是空字符串,这也可能引发此异常,正如您在{{1 }}
distance.get_jaro_distance
尝试将您的none类型和/或空字符串替换为'NA'或从数据集中过滤掉它们。
对可能引发此异常的行使用标志值/距离。在下面的示例中,我将使用if not first or not second:
raise JaroDistanceException("Cannot calculate distance from NoneType ({0}, {1})".format(
first.__class__.__name__,
second.__class__.__name__))
999