import pandas as pd import re df_a = pd.DataFrame({ "keyide" : ["a", "b", "c", "d"], "keyword" : ["numpy", "pandas", "matplotlib", "map"] }) df_b = pd.DataFrame({"senid" : range(10,19), "sentence" : [ "怎样用pan1das实现merge", "Python之Numpy详细教程", "怎么使用Pandas批量拆分与合并Excel文件?", "怎样使用pandas的map和apply函数?", "深度学习之tensorflow简介", "tensorflow和numpy的关系", "基于sklearn的一些机器学习的代码", "基于fghjfj的一些机器学习的代码", "落地鞥过来了干辣椒雷锋精神代理费据了解" ]}) key_word_dict = df_a.to_dict(orient="dict").get("keyword") df_b['keyids'] = df_b["sentence"].map(lambda sss: [keyid for keyid, key_word in key_word_dict.items() if re.search(key_word, sss, re.IGNORECASE)]) df_result = pd.merge( left = df_b.explode("keyids"), right = df_a, how='left', left_on = "keyids", right_on = df_a.index) print(df_result)
两个pandas模糊匹配
于 2022-12-22 20:38:19 首次发布