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

如何在Python Pandas中基于第二个DataFrame多列合并两个DataFrame

Pandas按优先级多字段匹配左连接实现方案

首先构造示例数据:

import pandas as pd

# 构造示例df1、df2
df1 = pd.DataFrame({'ID': [11, 22, 33, 44]})
df2 = pd.DataFrame({
    'ID1': [11, 88, 99],
    'ID2': [5, 22, 45],
    'ID3': [114, 18, 33]
})

方法一:映射字典法(适合小数据量,逻辑直观)

按优先级构建ID到df2行的映射,再直接匹配填充:

# 按优先级1、2、3依次构建匹配映射,高优先级不会被低优先级覆盖
match_map = {}
# 优先级1:匹配ID1
for _, row in df2.iterrows():
    if row['ID1'] not in match_map:
        match_map[row['ID1']] = row
# 优先级2:匹配ID2
for _, row in df2.iterrows():
    if row['ID2'] not in match_map:
        match_map[row['ID2']] = row
# 优先级3:匹配ID3
for _, row in df2.iterrows():
    if row['ID3'] not in match_map:
        match_map[row['ID3']] = row

# 匹配生成结果
result = df1.copy()
matched = result['ID'].map(match_map)
result[['ID1', 'ID2', 'ID3']] = pd.DataFrame(matched.tolist(), index=result.index)
# 匹配失败填充默认值123456
result = result.fillna(123456).astype(int)

方法二:左连接合并法(适合大数据量,性能更优)

通过三次左连接按优先级合并结果,避免循环:

# 按优先级依次左连接,用combine_first保留高优先级匹配结果
res = df1.merge(df2, left_on='ID', right_on='ID1', how='left')
res = res.combine_first(df1.merge(df2, left_on='ID', right_on='ID2', how='left'))
res = res.combine_first(df1.merge(df2, left_on='ID', right_on='ID3', how='left'))
# 填充默认值
res = res.fillna(123456).astype(int)

最终输出结果

两种方法得到的结果一致:

IDID1ID2ID3
11115114
22882218
33994533
44123456123456123456

内容的提问来源于stack exchange,提问作者dingaro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 16:09:02