You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按项目编码、位置列包含规则合并两个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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:54:04