你好,我有一个数据集,我想匹配我的关键字与位置。我遇到的问题是,在我的数据集中,“阿富汗”或“喀布尔”或“赫尔曼德”的位置以超过150种组合出现,包括拼写错误、大写和城市或城镇的名称。我想做的是创建一个单独的列,如果这些字符中的任何一个“afg”或“afg”或“kab”或“helm”或“helm”,则返回值1。我不确定大小写是否有区别
例如,有数百个这样的地点组合:杰格达拉克、阿富汗、阿富汗、加兹尼♥, 喀布尔/阿富汗
我试过这段代码,如果它与短语完全匹配是很好的,但是有太多的变化要写下每个异常
keywords= ['Afghanistan','Kabul','Herat','Jalalabad','Kandahar','Mazar-i-Sharif', 'Kunduz', 'Lashkargah', 'mazar', 'afghanistan','kabul','herat','jalalabad','kandahar']
#how to make a column that shows rows with a certain keyword..
def keyword_solution(value):
strings = value.split()
if any(word in strings for word in keywords):
return 1
else:
return 0
taleban_2['keyword_solution'] = taleban_2['location'].apply(keyword_solution)
# below will return the 1 values
taleban_2[taleban_2['keyword_solution'].isin(['1'])].head(5)
只需要替换这个逻辑,所有结果都将放入匹配“Afg”或“Afg”或“kab”或“kab”或“kund”或“kund”的“keyword\u solution”列中
鉴于以下情况:
list
或set
。我用set
是因为句子很长李>keywords
列表中'afgh' in ['afghanistan']
:False
'afgh' in 'afghanistan'
:True
word_list
的每个单词中搜索每个关键字李>[True if word in y else False for y in x for word in keywords]
afgh
,不需要afghanistan
)相关问题 更多 >
编程相关推荐