Merging DataFrames on Multiple Columns with Different Names in Pandas
19. Merge Multi-Col Diff Names
Write a Pandas program to merge DataFrames on multiple columns with different names.
In this exercise, we have merged two DataFrames on multiple columns where the columns have different names in each DataFrame.
Sample Solution :
Code :
import pandas as pd
# Create two sample DataFrames with matching values
df1 = pd.DataFrame({
'Employee_ID': [1, 2, 3],
'Name': ['Selena', 'Annabel', 'Caeso'] # Matching names with df2
})
df2 = pd.DataFrame({
'ID': [1, 2, 3],
'Employee_Name': ['Selena', 'Annabel', 'Caeso'], # Matching names with df1
'Salary': [50000, 60000, 70000]
})
# Merge the DataFrames on different column names
merged_df = pd.merge(df1, df2, left_on=['Employee_ID', 'Name'], right_on=['ID', 'Employee_Name'])
# Output the result
print(merged_df)
Output:
Employee_ID Name ID Employee_Name Salary 0 1 Selena 1 Selena 50000 1 2 Annabel 2 Annabel 60000 2 3 Caeso 3 Caeso 70000
Explanation:
- The DataFrames df1 and df2 now have matching values for 'Name' and 'Employee_Name'.
- The merge() function now correctly merges based on left_on=['Employee_ID', 'Name'] and right_on=['ID', 'Employee_Name'].
For more Practice: Solve these Related Problems:
- Write a Pandas program to merge two DataFrames on multiple columns with different names and validate the merge with a summary statistic.
- Write a Pandas program to merge two DataFrames on multiple columns with different names and create a new composite key column.
- Write a Pandas program to merge two DataFrames on multiple columns with different names and filter out rows with mismatched key data types.
- Write a Pandas program to merge two DataFrames on multiple columns with different names and then sort the result based on the composite key.
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.