Pandas Merge, Join and Concatenate Practice Questions with Solutions

Combining datasets is one of the most important tasks in data analysis. Pandas provides powerful functions such as merge(), join(), and concat() to combine multiple DataFrames efficiently. These operations are widely used in business reporting, customer analysis, sales management, finance, and machine learning projects. In this chapter, you’ll practice real-world questions on merging, joining, and concatenating DataFrames. Pandas Merge, Join and Concatenate practice questions with solutions help to build concepts.


1. Python Program to Merge Two DataFrames Using a Common Column

Problem Statement

Write a Python program to merge two DataFrames using the Employee_ID column.

Python Solution

import pandas as pd

employees = pd.DataFrame({
    "Employee_ID": [101, 102, 103],
    "Employee": ["Rahul", "Aman", "Priya"]
})

departments = pd.DataFrame({
    "Employee_ID": [101, 102, 103],
    "Department": ["IT", "HR", "Finance"]
})

result = pd.merge(
    employees,
    departments,
    on="Employee_ID"
)

print(result)

Sample Output

   Employee_ID Employee Department
0          101    Rahul         IT
1          102     Aman         HR
2          103    Priya    Finance

Explanation

The merge() function combines two DataFrames using the common Employee_ID column.

Concepts Covered

  • merge()
  • Common Column
  • Data Combination

2. Python Program to Perform an Inner Join

Problem Statement

Write a Python program to perform an inner join between two DataFrames.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "ID": [1, 2, 3],
    "Name": ["Rahul", "Aman", "Priya"]
})

df2 = pd.DataFrame({
    "ID": [2, 3, 4],
    "City": ["Delhi", "Mumbai", "Pune"]
})

result = pd.merge(
    df1,
    df2,
    on="ID",
    how="inner"
)

print(result)

Sample Output

   ID   Name    City
0   2   Aman   Delhi
1   3  Priya  Mumbai

Explanation

An inner join returns only the rows that have matching values in both DataFrames.

Concepts Covered

  • Inner Join
  • merge()
  • how="inner"

3. Python Program to Perform a Left Join

Problem Statement

Write a Python program to perform a left join between two DataFrames.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "ID": [1, 2, 3],
    "Name": ["Rahul", "Aman", "Priya"]
})

df2 = pd.DataFrame({
    "ID": [2, 3],
    "City": ["Delhi", "Mumbai"]
})

result = pd.merge(
    df1,
    df2,
    on="ID",
    how="left"
)

print(result)

Sample Output

   ID   Name    City
0   1  Rahul     NaN
1   2   Aman   Delhi
2   3  Priya  Mumbai

Explanation

A left join returns all rows from the left DataFrame and matching rows from the right DataFrame.

Concepts Covered

  • Left Join
  • merge()
  • Missing Values

4. Python Program to Perform a Right Join

Problem Statement

Write a Python program to perform a right join between two DataFrames.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "ID": [1, 2],
    "Name": ["Rahul", "Aman"]
})

df2 = pd.DataFrame({
    "ID": [2, 3],
    "City": ["Delhi", "Mumbai"]
})

result = pd.merge(
    df1,
    df2,
    on="ID",
    how="right"
)

print(result)

Sample Output

   ID   Name    City
0   2   Aman   Delhi
1   3    NaN  Mumbai

Explanation

A right join returns all rows from the right DataFrame and matching rows from the left DataFrame.

Concepts Covered

  • Right Join
  • merge()
  • Data Integration

5. Python Program to Perform an Outer Join

Problem Statement

Write a Python program to perform an outer join between two DataFrames.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "ID": [1, 2],
    "Name": ["Rahul", "Aman"]
})

df2 = pd.DataFrame({
    "ID": [2, 3],
    "City": ["Delhi", "Mumbai"]
})

result = pd.merge(
    df1,
    df2,
    on="ID",
    how="outer"
)

print(result)

Sample Output

   ID   Name    City
0   1  Rahul     NaN
1   2   Aman   Delhi
2   3    NaN  Mumbai

Explanation

An outer join returns all rows from both DataFrames and fills missing values with NaN where matches are unavailable.

Concepts Covered

  • Outer Join
  • merge()
  • Full Join

6. Python Program to Merge DataFrames Using Different Column Names

Problem Statement

Write a Python program to merge two DataFrames where the common columns have different names.

Python Solution

import pandas as pd

employees = pd.DataFrame({
    "Employee_ID": [101, 102, 103],
    "Employee": ["Rahul", "Aman", "Priya"]
})

salary = pd.DataFrame({
    "Emp_ID": [101, 102, 103],
    "Salary": [50000, 45000, 60000]
})

result = pd.merge(
    employees,
    salary,
    left_on="Employee_ID",
    right_on="Emp_ID"
)

print(result)

Sample Output

   Employee_ID Employee  Emp_ID  Salary
0          101    Rahul     101   50000
1          102     Aman     102   45000
2          103    Priya     103   60000

Explanation

The left_on and right_on parameters are used when the matching columns have different names.

Concepts Covered

  • merge()
  • left_on
  • right_on

7. Python Program to Concatenate Two DataFrames Row-Wise

Problem Statement

Write a Python program to concatenate two DataFrames vertically.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "Name": ["Rahul", "Aman"]
})

df2 = pd.DataFrame({
    "Name": ["Priya", "Sneha"]
})

result = pd.concat(
    [df1, df2],
    ignore_index=True
)

print(result)

Sample Output

     Name
0   Rahul
1    Aman
2   Priya
3  Sneha

Explanation

The concat() function joins DataFrames row by row.

Concepts Covered

  • concat()
  • Row-wise Concatenation
  • ignore_index

8. Python Program to Concatenate Two DataFrames Column-Wise

Problem Statement

Write a Python program to concatenate two DataFrames column-wise.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "Employee": ["Rahul", "Aman"]
})

df2 = pd.DataFrame({
    "Salary": [50000, 45000]
})

result = pd.concat(
    [df1, df2],
    axis=1
)

print(result)

Sample Output

  Employee  Salary
0    Rahul   50000
1     Aman   45000

Explanation

Using axis=1 concatenates DataFrames side by side.

Concepts Covered

  • concat()
  • axis=1
  • Column-wise Concatenation

9. Python Program to Join Two DataFrames Using Index

Problem Statement

Write a Python program to join two DataFrames using their indexes.

Python Solution

import pandas as pd

employees = pd.DataFrame({
    "Employee": ["Rahul", "Aman"]
})

salary = pd.DataFrame({
    "Salary": [50000, 45000]
})

result = employees.join(salary)

print(result)

Sample Output

  Employee  Salary
0    Rahul   50000
1     Aman   45000

Explanation

The join() function combines DataFrames based on their indexes.

Concepts Covered

  • join()
  • Index Join
  • Data Combination

10. Python Program to Merge Three DataFrames

Problem Statement

Write a Python program to merge three DataFrames using a common column.

Python Solution

import pandas as pd

employees = pd.DataFrame({
    "ID": [1, 2],
    "Employee": ["Rahul", "Aman"]
})

departments = pd.DataFrame({
    "ID": [1, 2],
    "Department": ["IT", "HR"]
})

salary = pd.DataFrame({
    "ID": [1, 2],
    "Salary": [50000, 45000]
})

result = pd.merge(
    employees,
    departments,
    on="ID"
)

result = pd.merge(
    result,
    salary,
    on="ID"
)

print(result)

Sample Output

   ID Employee Department  Salary
0   1    Rahul         IT   50000
1   2     Aman         HR   45000

Explanation

Multiple DataFrames can be merged one after another using the common key column.

Concepts Covered

  • merge()
  • Multiple DataFrames
  • Data Integration

11. Python Program to Merge DataFrames and Display the Source of Each Row

Problem Statement

Write a Python program to merge two DataFrames and identify whether each row comes from the left DataFrame, right DataFrame, or both.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "ID": [1, 2, 3],
    "Employee": ["Rahul", "Aman", "Priya"]
})

df2 = pd.DataFrame({
    "ID": [2, 3, 4],
    "City": ["Delhi", "Mumbai", "Pune"]
})

result = pd.merge(
    df1,
    df2,
    on="ID",
    how="outer",
    indicator=True
)

print(result)

Sample Output

   ID Employee    City      _merge
0   1    Rahul     NaN   left_only
1   2     Aman   Delhi        both
2   3    Priya  Mumbai        both
3   4      NaN    Pune  right_only

Explanation

The indicator=True parameter creates a new column named _merge, which shows the source of each row.

Concepts Covered

  • merge()
  • indicator=True
  • Outer Join

12. Python Program to Merge DataFrames with Duplicate Keys

Problem Statement

Write a Python program to merge DataFrames that contain duplicate key values.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "ID": [1, 1, 2],
    "Employee": ["Rahul", "Rohit", "Aman"]
})

df2 = pd.DataFrame({
    "ID": [1, 2],
    "Department": ["IT", "HR"]
})

result = pd.merge(
    df1,
    df2,
    on="ID"
)

print(result)

Sample Output

   ID Employee Department
0   1    Rahul         IT
1   1    Rohit         IT
2   2     Aman         HR

Explanation

When duplicate keys exist, Pandas creates matching combinations for each duplicate record.

Concepts Covered

  • Duplicate Keys
  • merge()
  • Data Relationships

13. Python Program to Concatenate Multiple DataFrames

Problem Statement

Write a Python program to concatenate three DataFrames into one DataFrame.

Python Solution

import pandas as pd

df1 = pd.DataFrame({"Marks": [80, 85]})
df2 = pd.DataFrame({"Marks": [90, 95]})
df3 = pd.DataFrame({"Marks": [75, 88]})

result = pd.concat(
    [df1, df2, df3],
    ignore_index=True
)

print(result)

Sample Output

   Marks
0     80
1     85
2     90
3     95
4     75
5     88

Explanation

The concat() function accepts a list of DataFrames and combines them into a single DataFrame.

Concepts Covered

  • concat()
  • Multiple DataFrames
  • Vertical Concatenation

14. Python Program to Join DataFrames with Different Indexes

Problem Statement

Write a Python program to join two DataFrames having different indexes.

Python Solution

import pandas as pd

employees = pd.DataFrame(
    {
        "Employee": ["Rahul", "Aman"]
    },
    index=[101, 102]
)

salary = pd.DataFrame(
    {
        "Salary": [50000, 45000]
    },
    index=[101, 102]
)

result = employees.join(salary)

print(result)

Sample Output

    Employee  Salary
101    Rahul   50000
102     Aman   45000

Explanation

The join() function combines DataFrames using their indexes.

Concepts Covered

  • join()
  • Index Join
  • Data Integration

15. Python Program to Merge DataFrames and Select Required Columns

Problem Statement

Write a Python program to merge two DataFrames and display only the required columns.

Python Solution

import pandas as pd

employees = pd.DataFrame({
    "ID": [1, 2],
    "Employee": ["Rahul", "Aman"]
})

salary = pd.DataFrame({
    "ID": [1, 2],
    "Salary": [50000, 45000]
})

result = pd.merge(
    employees,
    salary,
    on="ID"
)

print(result[["Employee", "Salary"]])

Sample Output

  Employee  Salary
0    Rahul   50000
1     Aman   45000

Explanation

After merging DataFrames, specific columns can be selected using column indexing.

Concepts Covered

  • merge()
  • Column Selection
  • Data Analysis

Chapter Summary

In this chapter, you learned how to combine multiple DataFrames using merge(), join(), and concat(). You practiced inner joins, left joins, right joins, outer joins, index-based joins, merging multiple DataFrames, concatenating rows and columns, handling duplicate keys, tracking merge sources with the _merge indicator, and selecting required columns after merging. These techniques are essential for integrating data from multiple sources in real-world analytics projects.


Key Takeaways

  • merge() combines DataFrames using common columns.
  • concat() combines DataFrames row-wise or column-wise.
  • join() combines DataFrames using indexes.
  • inner, left, right, and outer joins serve different purposes.
  • left_on and right_on handle different column names.
  • indicator=True identifies the source of merged rows.
  • Multiple DataFrames can be merged sequentially.
  • Duplicate keys generate multiple matching records.
  • ignore_index=True resets indexes after concatenation.
  • Merge, Join, and Concat are fundamental operations for data integration.

Frequently Asked Questions (FAQs)

1. What is the difference between merge() and concat()?

  • merge() combines DataFrames using common columns.
  • concat() stacks DataFrames either vertically or horizontally.

2. Which function joins DataFrames using indexes?

df1.join(df2)

3. How do you perform a left join?

pd.merge(df1, df2, on="ID", how="left")

4. How do you concatenate DataFrames vertically?

pd.concat([df1, df2], ignore_index=True)

5. How do you concatenate DataFrames horizontally?

pd.concat([df1, df2], axis=1)

6. What is the purpose of indicator=True?

It adds a column that indicates whether each row came from the left DataFrame, the right DataFrame, or both.


7. How do you merge DataFrames with different key column names?

pd.merge(
    df1,
    df2,
    left_on="Employee_ID",
    right_on="Emp_ID"
)

8. Why are Merge, Join, and Concat important in Pandas?

These functions allow analysts to combine data from multiple sources, build complete datasets, prepare data for reporting, perform business analysis, and create machine learning datasets efficiently.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top