pandas if多条件组合场景下精确匹配失效如何解决
问题根源
你的代码逻辑存在关联校验缺失的问题:两个过滤条件是独立判断的,df.first_unlist.str.match(pat=l.split(' ', 1)[0]).any() 仅判断df中存在任意行匹配当前句子首词,df.Second.str.match('noun').any() 仅判断整个df中存在任意行Second列值为noun,并没有要求匹配首词的那一行对应的Second列值为noun,因此只要df中本身存在Second为noun的行,所有首词匹配的句子都会被保留,导致过滤失效。
修正后的代码
你可以将两个条件合并为行筛选逻辑,同时判断同一行是否满足首词匹配、Second列值为noun,另外可将first_unlist的生成移到循环外避免重复计算,提升运行效率:
import pandas as pd # 原始DataFrame定义 data = {'First': [['First', 'value'],['second','value'],['third','value','is'],['fourth','value','is']], 'Second': ['noun','not noun','noun', 'not noun']} df = pd.DataFrame (data, columns = ['First','Second']) data2 = {'example': ['First value is important', 'second value is important too','it us good to know', 'Firstap is also good', 'aplsecond is very good']} df2 = pd.DataFrame (data2, columns = ['example']) # 预先生成拼接后的First列字段,无需重复计算 df['first_unlist'] = [','.join(map(str, l)) for l in df.First] def checker(): result =[] for sent in df2.example: # 提取当前句子首词 first_word = sent.split(' ', 1)[0] # 筛选同时满足首词匹配、Second列为noun的行,判断是否存在 has_match = (df.first_unlist.str.match(pat=first_word) & (df.Second == 'noun')).any() if has_match: result.append(sent) return result # 输出结果:['First value is important'] print(checker())
性能优化方案
如果数据量较大,你可以提前把所有Second为noun的行对应的首词提取为集合,循环时直接做集合成员判断即可,无需每次遍历整个DataFrame:
# 提前提取符合条件的首词集合 noun_head_words = set(row['First'][0] for _, row in df[df.Second == 'noun'].iterrows()) def checker_fast(): result = [] for sent in df2.example: sent_head = sent.split(' ', 1)[0] if sent_head in noun_head_words: result.append(sent) return result
内容的提问来源于stack exchange,提问作者zara kolagar
相关产品推荐
相关产品推荐

