w3resource

Merging DataFrames with overlapping column names in Pandas


Pandas: Custom Function Exercise-6 with Solution


Write a Pandas program that merges DataFrames with overlapping column names.

Following exercise shows how to merge two DataFrames that have overlapping column names, and specify how to handle those overlaps.

Sample Solution :

Code :

import pandas as pd

# Create two sample DataFrames with overlapping column names
df1 = pd.DataFrame({
    'ID': [1, 2, 3],
    'Name': ['Selena', 'Annabel', 'Caeso'],
    'Value': [100, 200, 300]
})

df2 = pd.DataFrame({
    'ID': [1, 2, 3],
    'Name': ['Selena', 'Annabel', 'Caeso'],
    'Value': [400, 500, 600]
})

# Merge the DataFrames, adding suffixes to overlapping columns
merged_df = pd.merge(df1, df2, on=['ID', 'Name'], suffixes=('_left', '_right'))

# Output the result
print(merged_df)

Output:

   ID     Name  Value_left  Value_right
0   1   Selena         100          400
1   2  Annabel         200          500
2   3    Caeso         300          600             

Explanation:

  • Created two DataFrames with overlapping column names ('ID', 'Name', and 'Value').
  • Used pd.merge() with the suffixes argument to handle overlapping column names.
  • The result includes both 'Value_left' and 'Value_right' for the overlapping columns.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.