Python 熊猫在具有不同名称的列上合并,避免重复

Python 熊猫在具有不同名称的列上合并,避免重复,python,pandas,merge,Python,Pandas,Merge,如何将两个数据帧合并到两个名称不同的列上,并保留其中一列 df1 = pd.DataFrame({'UserName': [1,2,3], 'Col1':['a','b','c']}) df2 = pd.DataFrame({'UserID': [1,2,3], 'Col2':['d','e','f']}) pd.merge(df1, df2, left_on='UserName', right_on='UserID') 这提供了这样一个数据帧 但很明显,我正在合并UserName和User

如何将两个数据帧合并到两个名称不同的列上,并保留其中一列

df1 = pd.DataFrame({'UserName': [1,2,3], 'Col1':['a','b','c']})
df2 = pd.DataFrame({'UserID': [1,2,3], 'Col2':['d','e','f']})
pd.merge(df1, df2, left_on='UserName', right_on='UserID')
这提供了这样一个数据帧

但很明显,我正在合并
UserName
UserID
,因此它们是相同的。我希望它看起来像这样。有什么干净的方法可以做到这一点吗

我唯一能想到的方法是在合并之前将列重新命名为相同的列,或者在合并之后删除其中一个列。如果熊猫自动掉落其中一只,我会很高兴,或者我可以做类似的事情

pd.merge(df1, df2, left_on='UserName', right_on='UserID', keep_column='left')

这里面没有什么好东西:它的意思是保留列,因为较大的案例(如左-右或外部联接)会为两列带来额外的信息。不要试图过度设计你的合并行,要像你建议的那样明确

解决方案1:

df2.columns = ['Col2', 'UserName']

pd.merge(df1, df2,on='UserName')
Out[67]: 
  Col1  UserName Col2
0    a         1    d
1    b         2    e
2    c         3    f
解决方案2:

pd.merge(df1, df2, left_on='UserName', right_on='UserID').drop('UserID', axis=1)
Out[71]: 
  Col1  UserName Col2
0    a         1    d
1    b         2    e
2    c         3    f

UserID
设置为索引,然后在第二个数据帧的索引上加入如何

pd.merge(df1, df2.set_index('UserID'), left_on='UserName', right_index=True)

#   Col1    UserName    Col2
# 0    a           1       d
# 1    b           2       e
# 2    c           3       f

回答得很好。我几乎完全按照OP做的做了,得到了多余的专栏。从您的代码中阅读,我认为如果我通过索引连接左侧和右侧,该列将排序为“合并”到索引列中,因此不会显示在结果中?谢谢