如何按项目编码、位置列包含规则合并两个Pandas DataFrame
实现方案
核心逻辑是先按Project_Code关联两张表,再过滤满足df1.Location包含df2.Location_City的记录,刚好满足你要求的两个字段同时匹配才能唯一识别的规则。
基础实现(适合中小数据量)
import pandas as pd # 第一步:按Project_Code做内连接,先保证项目编码匹配 merged_df = df1.merge(df2, on="Project_Code", how="inner") # 第二步:过滤位置满足包含关系的记录 final_result = merged_df[merged_df.apply(lambda row: row["Location_City"] in row["Location"], axis=1)]
优化实现(适合百万级以上数据量)
逐行apply性能较低,可以用列表推导替换,速度提升3-5倍:
final_result = merged_df[ [city in loc for city, loc in zip(merged_df["Location_City"], merged_df["Location"])] ]
常见场景适配
- 若存在大小写不匹配的需求,可统一转小写后判断:
final_result = merged_df[ [city.lower() in loc.lower() for city, loc in zip(merged_df["Location_City"], merged_df["Location"])] ] - 若
Location_City包含.、*等正则特殊字符,用str.contains时需关闭正则匹配:final_result = merged_df[ merged_df.apply(lambda row: row["Location"].contains(row["Location_City"], regex=False), axis=1) ]
内容的提问来源于stack exchange,提问作者SOScodehelp
相关产品推荐
相关产品推荐

