我的目标是找到与列general_text
中的行匹配的City
,但匹配必须是精确的。你知道吗
我尝试使用搜索IN
,但是它没有给我预期的结果,所以我尝试使用str.contain
,但是我尝试的方式显示了一个错误。关于如何正确或有效地做这件事有什么提示吗?你知道吗
df['matched'] = df.apply(lambda x: x.City in x.general_text, axis=1)
但结果如下:
data = [['palm springs john smith':'spring'],
['palm springs john smith':'palm springs'],
['palm springs john smith':'smith'],
['hamptons amagansett':'amagansett'],
['hamptons amagansett':'hampton'],
['hamptons amagansett':'gans'],
['edward riverwoods lake':'wood'],
['edward riverwoods lake':'riverwoods']]
df = pd.DataFrame(data, columns = [ 'general_text':'City'])
df['match'] = df.apply(lambda x: x['general_text'].str.contain(
x.['City']), axis = 1)
我希望上面的代码只匹配以下内容:
data = [['palm springs john smith':'palm springs'],
['hamptons amagansett':'amagansett'],
['edward riverwoods lake':'riverwoods']]
可以使用单词边界
\b\b
进行精确匹配:或:
相关问题 更多 >
编程相关推荐