Pandas Merge, Join, and Concatenate Practice Questions with Solutions

Combining multiple datasets is a common task in data analysis. Pandas provides powerful functions such as merge(), join(), and concat() to combine DataFrames based on columns or indexes. These functions are widely used in data cleaning, reporting, business intelligence, and machine learning projects where information comes from multiple sources. In this chapter, you’ll learn how to merge, join, and concatenate DataFrames using practical examples. Pandas Merge, Join, and Concatenate practice questions with solutions help to understand the concepts.


1. Python Program to Concatenate Two DataFrames Vertically

Problem Statement

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

Python Solution

import pandas as pd

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

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

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

print(result)

Sample Output

     Name  Marks
0   Rahul     85
1    Aman     90
0   Priya     78
1   Sneha     88

Explanation

The concat() function combines multiple DataFrames vertically by default.

Concepts Covered

  • concat()
  • Vertical Concatenation
  • Data Combination

2. Python Program to Concatenate Two DataFrames Horizontally

Problem Statement

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

Python Solution

import pandas as pd

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

df2 = pd.DataFrame({
    "Marks": [85, 90]
})

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

print(result)

Sample Output

     Name  Marks
0   Rahul     85
1    Aman     90

Explanation

Setting axis=1 combines DataFrames horizontally by adding columns.

Concepts Covered

  • concat()
  • Horizontal Concatenation
  • Axis Parameter

3. Python Program to Ignore Index While Concatenating

Problem Statement

Write a Python program to concatenate two DataFrames and create a new continuous index.

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 ignore_index=True parameter resets the index after concatenation.

Concepts Covered

  • concat()
  • ignore_index
  • Index Management

4. Python Program to Merge Two DataFrames on a Common Column

Problem Statement

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

Python Solution

import pandas as pd

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

marks = pd.DataFrame({
    "ID": [1, 2, 3],
    "Marks": [85, 90, 78]
})

result = pd.merge(
    students,
    marks,
    on="ID"
)

print(result)

Sample Output

   ID   Name  Marks
0   1  Rahul     85
1   2   Aman     90
2   3  Priya     78

Explanation

The merge() function combines two DataFrames based on the matching values in the specified column.

Concepts Covered

  • merge()
  • Common Column
  • Data Integration

5. Python Program to Perform an Inner Join Using merge()

Problem Statement

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

Python Solution

import pandas as pd

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

marks = pd.DataFrame({
    "ID": [2, 3, 4],
    "Marks": [90, 78, 88]
})

result = pd.merge(
    students,
    marks,
    on="ID",
    how="inner"
)

print(result)

Sample Output

   ID   Name  Marks
0   2   Aman     90
1   3  Priya     78

Explanation

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

Concepts Covered

  • merge()
  • Inner Join
  • Common Records

6. Python Program to Perform a Left Join Using merge()

Problem Statement

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

Python Solution

import pandas as pd

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

marks = pd.DataFrame({
    "ID": [2, 3],
    "Marks": [90, 78]
})

result = pd.merge(
    students,
    marks,
    on="ID",
    how="left"
)

print(result)

Sample Output

   ID   Name  Marks
0   1  Rahul    NaN
1   2   Aman   90.0
2   3  Priya   78.0

Explanation

A left join returns all rows from the left DataFrame and matching rows from the right DataFrame. If no match exists, Pandas fills the missing values with NaN.

Concepts Covered

  • merge()
  • Left Join
  • Missing Values

7. Python Program to Perform a Right Join Using merge()

Problem Statement

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

Python Solution

import pandas as pd

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

marks = pd.DataFrame({
    "ID": [1, 2, 3],
    "Marks": [85, 90, 78]
})

result = pd.merge(
    students,
    marks,
    on="ID",
    how="right"
)

print(result)

Sample Output

   ID   Name  Marks
0   1    NaN     85
1   2   Aman     90
2   3  Priya     78

Explanation

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

Concepts Covered

  • merge()
  • Right Join
  • Data Integration

8. Python Program to Perform an Outer Join Using merge()

Problem Statement

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

Python Solution

import pandas as pd

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

marks = pd.DataFrame({
    "ID": [2, 3],
    "Marks": [90, 78]
})

result = pd.merge(
    students,
    marks,
    on="ID",
    how="outer"
)

print(result)

Sample Output

   ID   Name  Marks
0   1  Rahul    NaN
1   2   Aman   90.0
2   3    NaN   78.0

Explanation

An outer join returns all rows from both DataFrames. Missing matches are filled with NaN.

Concepts Covered

  • merge()
  • Outer Join
  • Full Data Combination

9. Python Program to Merge DataFrames with 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

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

marks = pd.DataFrame({
    "ID": [1, 2, 3],
    "Marks": [85, 90, 78]
})

result = pd.merge(
    students,
    marks,
    left_on="Student_ID",
    right_on="ID"
)

print(result)

Sample Output

   Student_ID   Name  ID  Marks
0           1  Rahul   1     85
1           2   Aman   2     90
2           3  Priya   3     78

Explanation

The left_on and right_on parameters allow merging DataFrames when the key columns have different names.

Concepts Covered

  • merge()
  • left_on
  • right_on

10. 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

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

df2 = pd.DataFrame(
    {
        "Marks": [85, 90, 78]
    },
    index=[1, 2, 3]
)

result = df1.join(df2)

print(result)

Sample Output

     Name  Marks
1   Rahul     85
2    Aman     90
3   Priya     78

Explanation

The join() function combines DataFrames using their indexes by default.

Concepts Covered

  • join()
  • Index Join
  • Data Combination

11. Python Program to Join DataFrames with a Left Join Using join()

Problem Statement

Write a Python program to perform a left join using the join() function.

Python Solution

import pandas as pd

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

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

result = employees.join(salary, how="left")

print(result)

Sample Output

      Name   Salary
101  Rahul  50000.0
102   Aman      NaN
103  Priya  55000.0

Explanation

The join() function performs a left join by default and matches rows using the DataFrame indexes.

Concepts Covered

  • join()
  • Left Join
  • Index-Based Joining

12. Python Program to Concatenate Multiple DataFrames

Problem Statement

Write a Python program to concatenate three DataFrames vertically.

Python Solution

import pandas as pd

df1 = pd.DataFrame({"Marks": [85, 90]})
df2 = pd.DataFrame({"Marks": [78, 88]})
df3 = pd.DataFrame({"Marks": [91, 82]})

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

print(result)

Sample Output

   Marks
0     85
1     90
2     78
3     88
4     91
5     82

Explanation

The concat() function accepts a list of DataFrames, making it easy to combine multiple datasets.

Concepts Covered

  • concat()
  • Multiple DataFrames
  • Vertical Concatenation

13. Python Program to Add Keys While Concatenating

Problem Statement

Write a Python program to concatenate DataFrames with custom keys.

Python Solution

import pandas as pd

df1 = pd.DataFrame({"Marks": [85, 90]})
df2 = pd.DataFrame({"Marks": [78, 88]})

result = pd.concat(
    [df1, df2],
    keys=["Class A", "Class B"]
)

print(result)

Sample Output

           Marks
Class A 0     85
        1     90
Class B 0     78
        1     88

Explanation

The keys parameter creates a hierarchical index that identifies the source DataFrame.

Concepts Covered

  • concat()
  • Keys
  • MultiIndex

14. Python Program to Merge DataFrames with Indicator Column

Problem Statement

Write a Python program to merge two DataFrames and display the source of each row.

Python Solution

import pandas as pd

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

marks = pd.DataFrame({
    "ID": [2, 3],
    "Marks": [90, 78]
})

result = pd.merge(
    students,
    marks,
    on="ID",
    how="outer",
    indicator=True
)

print(result)

Sample Output

   ID   Name  Marks      _merge
0   1  Rahul    NaN   left_only
1   2   Aman   90.0        both
2   3    NaN   78.0  right_only

Explanation

The indicator=True parameter adds a column showing whether the row came from the left DataFrame, the right DataFrame, or both.

Concepts Covered

  • merge()
  • indicator=True
  • Merge Source Tracking

15. Python Program to Merge DataFrames on Multiple Columns

Problem Statement

Write a Python program to merge two DataFrames using Department and Year as common columns.

Python Solution

import pandas as pd

df1 = pd.DataFrame({
    "Department": ["IT", "IT", "HR"],
    "Year": [2024, 2025, 2024],
    "Students": [50, 55, 40]
})

df2 = pd.DataFrame({
    "Department": ["IT", "HR", "IT"],
    "Year": [2024, 2024, 2025],
    "Faculty": [10, 8, 12]
})

result = pd.merge(
    df1,
    df2,
    on=["Department", "Year"]
)

print(result)

Sample Output

  Department  Year  Students  Faculty
0         IT  2024        50       10
1         IT  2025        55       12
2         HR  2024        40        8

Explanation

The merge() function accepts multiple columns in the on parameter, allowing joins based on composite keys.

Concepts Covered

  • merge()
  • Multiple Key Merge
  • Composite Keys

16. Python Program to Concatenate DataFrames with Different Columns

Problem Statement

Write a Python program to concatenate two DataFrames that have different column names.

Python Solution

import pandas as pd

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

df2 = pd.DataFrame({
    "Marks": [85, 90]
})

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

print(result)

Sample Output

     Name  Marks
0   Rahul    NaN
1    Aman    NaN
2     NaN   85.0
3     NaN   90.0

Explanation

When concatenating DataFrames with different column names, Pandas automatically creates missing columns and fills unavailable values with NaN.

Concepts Covered

  • concat()
  • Different Columns
  • Missing Values

17. Python Program to Merge Customer and Order Data

Problem Statement

Write a Python program to merge customer details with order information using the Customer_ID column.

Python Solution

import pandas as pd

customers = pd.DataFrame({
    "Customer_ID": [101, 102, 103],
    "Customer_Name": ["Rahul", "Aman", "Priya"]
})

orders = pd.DataFrame({
    "Customer_ID": [101, 103, 104],
    "Order_Amount": [2500, 4200, 3100]
})

result = pd.merge(
    customers,
    orders,
    on="Customer_ID",
    how="left"
)

print(result)

Sample Output

   Customer_ID Customer_Name  Order_Amount
0          101         Rahul        2500.0
1          102          Aman           NaN
2          103         Priya        4200.0

Explanation

This example demonstrates a real-world use case where customer records are merged with order details. Customers without matching orders receive NaN values.

Concepts Covered

  • merge()
  • Left Join
  • Real-World Data Integration

Chapter Summary

In this chapter, you learned how to combine multiple DataFrames using concat(), merge(), and join(). You explored vertical and horizontal concatenation, index management, inner, left, right, and outer joins, merging using multiple columns, joining on indexes, using custom keys, tracking merge sources with the indicator parameter, and working with DataFrames having different column structures. These techniques are widely used in data engineering, reporting, business intelligence, and machine learning projects.


Key Takeaways

  • concat() combines DataFrames vertically or horizontally.
  • ignore_index=True creates a continuous index after concatenation.
  • merge() joins DataFrames using common columns.
  • join() combines DataFrames using indexes.
  • how="inner" returns only matching records.
  • how="left" keeps all rows from the left DataFrame.
  • how="right" keeps all rows from the right DataFrame.
  • how="outer" keeps all rows from both DataFrames.
  • left_on and right_on merge DataFrames with different key names.
  • indicator=True identifies the source of merged records.

Frequently Asked Questions (FAQs)

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

  • merge() combines DataFrames based on common columns.
  • concat() simply appends DataFrames vertically or horizontally.

2. Which function joins DataFrames using indexes?

Use the join() function.

df1.join(df2)

3. How do you perform a left join in Pandas?

Use the how="left" parameter.

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

4. What is the purpose of ignore_index=True?

It resets the index after concatenating multiple DataFrames.

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

5. Can Pandas merge DataFrames using multiple columns?

Yes. Pass a list of column names to the on parameter.

pd.merge(
    df1,
    df2,
    on=["Department", "Year"]
)

6. What does indicator=True do in merge()?

It adds a new column named _merge showing whether each row came from the left DataFrame, right DataFrame, or both.


7. What happens if two DataFrames have different columns while using concat()?

Pandas automatically creates the missing columns and fills unavailable values with NaN.


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

These operations allow you to combine data from multiple sources, making them essential for data cleaning, reporting, business intelligence, ETL pipelines, and machine learning workflows.

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

Scroll to Top