在pandas中使用apply抽取关键词从描述字段中查看哪些关键词被命中,并形成一个新的列使用第二列匹配第三列代码如下importreimportpandasaspd#ImportPathfromsettingimportDataPathfromosimportpathdefmingzhong(das):keywords=['关键字1','关键字2','关键字3'....]key_box=[]forkeywordinkeywords:pattern=re.compile('.*'+keyword+'.*')如果pattern.match(das["description"])不是None:key_box.append(keyword)print(key_box)returnkey_boxdf1=pd.read_excel(path.join(DataPath,'待处理文件.xlsx'),sheet_name="sheet页面名称",usecols='L,AL')df1["打关键词"]=df1.apply(mingzhong,axis=1)writer=pd.ExcelWriter(path.join(DataPath,'hit.xlsx'))df1.to_excel(writer,sheet_name='sheet1',index=False)writer.save()writer.close()
