w3resource

Merging DataFrames with overlapping column names in Pandas


6. Merge Overlapping Columns

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.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to merge two DataFrames with overlapping column names and automatically rename the duplicates.
  • Write a Pandas program to merge two DataFrames with overlapping columns and then select only the columns originating from the left DataFrame.
  • Write a Pandas program to merge two DataFrames with overlapping columns using custom suffixes and filter rows based on one of the renamed columns.
  • Write a Pandas program to merge two DataFrames with overlapping column names and compute a new column based on operations performed on these 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.



Follow us on Facebook and Twitter for latest update.