w3resource

Handling overlapping columns in Pandas merge using suffixes


14. Merge with Suffixes

Write a Pandas program to merge DataFrames using suffixes for overlapping columns.

In this exercise, we have handled overlapping columns during a merge by specifying custom suffixes.

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'],
    'Age': [25, 30, 22]
})

df2 = pd.DataFrame({
    'ID': [1, 2, 3],
    'Name': ['Selena', 'Annabel', 'Caeso'],
    'Age': [27, 31, 23]
})

# Merge the DataFrames, handling overlapping 'Age' column with suffixes
merged_df = pd.merge(df1, df2, on=['ID', 'Name'], suffixes=('_left', '_right'))

# Output the result
print(merged_df)

Output:

   ID     Name  Age_left  Age_right
0   1   Selena        25         27
1   2  Annabel        30         31
2   3    Caeso        22         23  

Explanation:

  • Created two DataFrames df1 and df2 with overlapping column names ('ID', 'Name', 'Age').
  • Used pd.merge() and the suffixes argument to specify custom suffixes for the overlapping 'Age' column.
  • The result includes both 'Age_left' and 'Age_right' to differentiate between the two DataFrames.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to merge two DataFrames using suffixes for overlapping columns and compare the values of the duplicated columns.
  • Write a Pandas program to merge two DataFrames using suffixes for overlapping columns and then drop one set of the duplicated columns.
  • Write a Pandas program to merge two DataFrames using suffixes and subsequently rename the resulting columns to remove the suffixes.
  • Write a Pandas program to merge two DataFrames using suffixes for overlapping columns and compute the average of these 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.



Follow us on Facebook and Twitter for latest update.