Reputation: 9369
The pandas merge()
function allows to add suffixes to overlapping column names:
merged = table1.merge(table2, left_on='header', right_on='header',
suffixes=('table1', 'table2'))
However, this adds suffixes only to the overlapping columns. Is it possible to add a suffix to all columns except the merge column?
Upvotes: 22
Views: 20429
Reputation: 24261
You could add the suffixes to the tables before merging, and revert the merge column names:
table1 = table1.add_suffix('table1')
table1 = table1.rename(index=str, columns={'headertable1':'header'})
table2 = table2.add_suffix('table2')
table2 = table2.rename(index=str, columns={'headertable2':'header'})
merged = table1.merge(table2, on='header')
Upvotes: 16