我試圖辨別兩個字符串之間的字符串相似性(使用 Jaro)。每個字符串都位于我的數據框中的單獨列中。String 1 = df['name_one'] String 2 = df['name_two']當我嘗試運行字符串相似性邏輯時:from pyjarowinkler import distancedf['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'] == Nonedf[maskone]masktwo = df['name_two'] == Nonedf[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?
1 回答

互換的青春
TA貢獻1797條經驗 獲得超6個贊
問題
問題并不完全是您只遇到NoneTypes
空字符串,它也可能引發此異常,正如您在實現中看到的那樣distance.get_jaro_distance
if not first or not second:
raise JaroDistanceException("Cannot calculate distance from NoneType ({0}, {1})".format(
first.__class__.__name__,
second.__class__.__name__))
選項1
嘗試用“NA”替換您的無類型和/或空字符串或從數據集中過濾它們。
選項2
對可能引發此異常的行使用標志值/距離。在下面的例子中,我將利用999
from pyjarowinkler import distance
df['distance'] = df.apply(lambda d: 999 if not str(d['name_one']) or not str(d['name_two']) else distance.get_jaro_distance(str(d['name_one']),str(d['name_two']),winkler=True,scaling=0.1), axis=1)
添加回答
舉報
0/150
提交
取消