How can I combine two pandas DataFrames in two columns with different names and save one of the columns?
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')
It provides a DataFrame similar to this.

But it’s clear that I am merging on UserNameand UserIDso that they are the same. I want it to look like this. Are there any clean ways to do this?

Only the ways I can think of are either to rename the columns so that they are the same before the merge, or discard one of them after the merge. I would be happy if pandas automatically deleted one of them or I could do something like
pd.merge(df1, df2, left_on='UserName', right_on='UserID', keep_column='left')
source
share