Pandas Data Merging (merge / join)
Pandas provides powerful data merging capabilities, allowing you to join two or more DataFrames by keys just like in SQL.merge and join are the two most commonly used methods.
Basic usage of merge
pd.merge() The function merges two DataFrames by columns, similar to a SQL JOIN operation.
Simple merge
Example
import
pandas
as
pd
Create two DataFrames
df1
=
pd.
DataFrame
(
{
"Student ID"
:
[
"S001"
,
"S002"
,
"S003"
]
,
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
]
}
)
df2
=
pd.
DataFrame
(
{
"Student ID"
:
[
"S001"
,
"S002"
,
"S003"
]
,
"Math"
:
[
85
,
92
,
78
]
}
)
(
"DataFrame 1:"
)
(
df1
)
(
)
(
"DataFrame 2:"
)
(
df2
)
(
)
Collapse
result
=
pd.
merge
(
df1
,
df2
,
on
=
"Student ID"
)
(
"Merge result:"
)
(
result
)
Merge with different column names
Example
import
pandas
as
pd
df1
=
pd.
DataFrame
(
{
"Student ID"
:
[
"S001"
,
"S002"
,
"S003"
]
,
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
]
}
)
df2
=
pd.
DataFrame
(
{
"student_id"
:
[
"S001"
,
"S002"
,
"S003"
]
,
"Math"
:
[
85
,
92
,
78
]
}
)
Using left_on and right_on
result
=
pd.
merge
(
df1
,
df2
,
left_on
=
"Student ID"
,
right_on
=
"student_id"
)
(
"Merge with different column names:"
)
(
result
)
(
)
Drop redundant columns
result
=
result.
drop
(
"student_id"
,
axis
=
1
)
(
"After dropping redundant columns:"
)
(
result
)
Merge types
inner / left / right / outer
Example
import
pandas
as
pd
df1
=
pd.
DataFrame
(
{
"Student ID"
:
[
"S001"
,
"S002"
,
"S003"
,
"S004"
]
,
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
]
}
)
df2
=
pd.
DataFrame
(
{
"Student ID"
:
[
"S001"
,
"S002"
,
"S003"
,
"S005"
]
,
"Math"
:
[
85
,
92
,
78
,
88
]
}
)
(
"DataFrame 1:"
)
(
df1
)
(
"\nDataFrame 2:"
)
(
df2
)
(
)
inner join (default): keep only rows present in both
(
"inner join (intersection):"
)
(
pd.
merge
(
df1
,
df2
,
on
=
"Student ID"
,
how
=
"inner"
)
)
(
)
left join: keep all rows from the left table
(
"left join (keep left table):"
)
(
pd.
merge
(
df1
,
df2
,
on
=
"Student ID"
,
how
=
"left"
)
)
(
)
right join: keep all rows from the right table
(
"right join (keep right table):"
)
(
pd.
merge
(
df1
,
df2
,
on
=
"Student ID"
,
how
=
"right"
)
)
(
)
outer join: keep all rows
(
"outer join (union):"
)
(
pd.
merge
(
df1
,
df2
,
on
=
"Student ID"
,
how
=
"outer"
)
)
It is important to understand the differences among the four JOIN types: inner keeps the intersection, left keeps all rows of the left table, right keeps all rows of the right table, and outer keeps all rows of both tables.
Merge on multiple keys
Example
import
pandas
as
pd
df1
=
pd.
DataFrame
(
{
"City"
:
[
"Beijing"
,
"Beijing"
,
"Shanghai"
,
"Shanghai"
]
,
"Year"
:
[
2023
,
2024
,
2023
,
2024
]
,
"Sales"
:
[
100
,
120
,
90
,
110
]
}
)
df2
=
pd.
DataFrame
(
{
"City"
:
[
"Beijing"
,
"Beijing"
,
"Shanghai"
,
"Shanghai"
]
,
"Year"
:
[
2023
,
2024
,
2023
,
2024
]
,
"Profit"
:
[
20
,
25
,
18
,
22
]
}
)
Merge on multiple keys
result
=
pd.
merge
(
df1
,
df2
,
on
=
[
"City"
,
"Year"
]
)
(
"Merge on multiple keys:"
)
(
result
)
The join method
DataFrame.join() is another way to merge, defaulting to a left join and merging by index.
Merge by index
Example
import
pandas
as
pd
Create data and set the index
df1
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
]
,
"Age"
:
[
25
,
30
,
28
]
}
,
index
=
[
"S001"
,
"S002"
,
"S003"
]
)
df2
=
pd.
DataFrame
(
{
"Math"
:
[
85
,
92
,
78
]
,
"English"
:
[
90
,
88
,
95
]
}
,
index
=
[
"S001"
,
"S002"
,
"S003"
]
)
(
"DataFrame 1:"
)
(
df1
)
(
)
(
"DataFrame 2:"
)
(
df2
)
(
)
Merge using join
result
=
df1.
join
(
df2
)
(
"join merge:"
)
(
result
)
Merge multiple tables
Example
import
pandas
as
pd
df1
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
]
}
,
index
=
[
"S001"
,
"S002"
,
"S003"
]
)
df2
=
pd.
DataFrame
(
{
"Math"
:
[
85
,
92
,
78
]
}
,
index
=
[
"S001"
,
"S002"
,
"S004"
]
)
df3
=
pd.
DataFrame
(
{
"English"
:
[
90
,
88
,
95
]
}
,
index
=
[
"S001"
,
"S003"
,
"S004"
]
)
Chained join
result
=
df1.
join
(
df2
,
how
=
"left"
)
.
join
(
df3
,
how
=
"left"
)
(
"Multiple-table join (left join):"
)
(
result
)
(
)
Using outer
result2
=
df1.
join
(
df2
,
how
=
"outer"
)
.
join
(
df3
,
how
=
"outer"
)
(
"Multiple-table join (full outer join):"
)
(
result2
)
Practical example: joining multiple tables
Example
import
pandas
as
pd
Simulate business data
1. Employee table
employees
=
pd.
DataFrame
(
{
"Employee ID"
:
[
"E001"
,
"E002"
,
"E003"
,
"E004"
]
,
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
]
,
"Department ID"
:
[
"D001"
,
"D001"
,
"D002"
,
"D003"
]
}
)
2. Department table
departments
=
pd.
DataFrame
(
{
"Department ID"
:
[
"D001"
,
"D002"
,
"D003"
]
,
"Department Name"
:
[
"Engineering Department"
,
"Sales Department"
,
"Operations Department"
]
}
)
3. Salary table
salaries
=
pd.
DataFrame
(
{
"Employee ID"
:
[
"E001"
,
"E002"
,
"E003"
,
"E004"
]
,
"Salary"
:
[
12000
,
15000
,
11000
,
18000
]
}
)
(
"Step 1: Merge employees and departments"
)
result
=
pd.
merge
(
employees
,
departments
,
on
=
"Department ID"
)
(
result
)
(
)
(
"Step 2: Then merge salaries"
)
result
=
pd.
merge
(
result
,
salaries
,
on
=
"Employee ID"
)
(
result
)
Choosing between merge and join
| compares | merge | join |
|---|---|---|
| Merge basis | column | Index |
| Default join | inner | left |
| Applicable scenarios | Merge by business key | Merge by primary key/index |
In most cases merge is more flexible, while join is more concise when merging by index. Choose the appropriate method based on your actual data structure.