本文介绍一种高效、简洁的方法,使用 pandas 的 `concat` 与分组聚合实现多表按公司名称(支持近似匹配)智能拼接,并自动处理列冲突(如重复值存为元组

在实际数据分析中,我们常需整合来自不同来源的公司数据表——这些表共享“Company Name”列,但名称存在拼写差异(如 "A corp" vs "A Corporation")、大小写不一致或空格冗余;同时各表字段不完全重叠(如有的含 Value 和 Currency,有的含 HQ 和 Website)。直接使用 pd.merge 或 pd.concat 无法解决模糊匹配问题,而手动遍历 + fuzzywuzzy 易出错、性能差且逻辑复杂。
更优解是先标准化名称,再借助索引对齐与分组聚合完成智能合并。核心思路如下:
以下是完整可运行示例代码:
import pandas as pd
import numpy as np
# 示例数据(对应问题中的四张表)
df1 = pd.DataFrame({'Company Name': ['A corp', 'B corp'], 'Value': ['Cell 2', 'Cell 4']})
df2 = pd.DataFrame({
'Company Name': ['A corp', 'C corp'],
'Date': ['Cell 2', 'Cell 4'],
'Leadership': ['Cell 1', 'Cell 1'],
'Net Profit': ['Cell 2', 'Cell 2']
})
df3 = pd.DataFrame({'Company Name': ['A corp', 'B corp', 'C corp'], 'Value': ['Cell 2', 'Cell 4', 'Cell 4']})
df4 = pd.DataFrame({
'Company Name': ['D corp', 'D corp'],
'Percentage': ['Cell 2', 'Cell 4'],
'Comment': ['Cell 2', 'Cell 4'],
'HQ': ['Cell 2', 'Cell 4'],
'Website': ['Cell 2', 'Cell 4']
})
dfs = [df1, df2, df3, df4]
def aggregate_nonnull(s):
s_clean = s.dropna()
if len(s_clean) > 1:
return tuple(s_clean.unique()) # 去重后转元组,避免重复项
elif len(s_clean) == 1:
return s_clean.iloc[0]
else:
return np.nan
# 关键步骤:设置双层索引 + 水平拼接 + 分组聚合
combined = (
pd.concat([
d.set_index(['Company Name', d.groupby('Company Name').cumcount()])
for d in dfs
], axis=1)
.T.groupby(level=0).agg(aggregate_nonnull).T
.reset_index('Company Name')
)
print(combined)✅ 输出说明:
⚠️ 注意事项:
该方案兼顾简洁性、可读性与工程鲁棒性,是处理多源异构公司数据融合的推荐实践。