w3resource

Merging DataFrames with duplicate Keys in Pandas

Pandas: Custom Function Exercise-9 with Solution

Write a Pandas program to merge DataFrames with duplicate Keys.

Following program shows how to merge DataFrames that contain duplicate keys, resulting in a Cartesian product of matching rows.

Sample Solution :

Code :

import pandas as pd

# Create two sample DataFrames with duplicate keys
df1 = pd.DataFrame({
    'ID': [1, 1, 2],
    'Name': ['Annabel', 'Annabel', 'Selena']
})

df2 = pd.DataFrame({
    'ID': [1, 2],
    'Age': [25, 30]
})

# Merge the DataFrames with duplicate keys
merged_df = pd.merge(df1, df2, on='ID')

# Output the result
print(merged_df)

Output:

   ID     Name  Age
0   1  Annabel   25
1   1  Annabel   25
2   2   Selena   30        

Explanation:

  • Created two DataFrames df1 and df2 with duplicate keys in df1.
  • Used pd.merge() to merge on the 'ID' column.
  • The result is a Cartesian product where matching keys from both DataFrames are combined.

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.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/pandas/pandas-merge-dataframes-with-duplicate-keys.php