Categories
merge pandas python

Only copy one key-column into merged DataFrame

Consider the following DataFrames:

df1 = pd.DataFrame({'a': [0, 1, 2, 3], 'b': list('abcd')})
df2 = pd.DataFrame({'c': list('abcd'), 'd': 'Alex'})

In this instance, df1['b'] and df2['c'] are the key columns. So when merging:

df1.merge(df2, left_on='b', right_on='c')
a b c d
0 0 a a Alex
1 1 b b Alex
2 2 c c Alex
3 3 d d Alex

I end up with both key columns in the resultant DataFrame when I only need one. I’ve been using:

df1.merge(df2, left_on='b', right_on='c').drop('c', axis="columns")

Is there a way to only keep one key column?