Concatenate, Merge & Join
Pandas Basics
1 min read
This section is 1 min read, full guide is 30 min read
Published Sep 29 2025, updated Oct 24 2025
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
PandasPython
Concatenate (pd.concat)
Think of this as stacking DataFrames either vertically (rows) or horizontally (columns).
Syntax:
pd.concat(objs, axis=0, join="outer", ignore_index=False, keys=None)
Copy to Clipboard
Key Parameters
objs→ list/tuple of DataFrames.axis=0→ stack rows (default).axis=1→ stack columns (side by side).ignore_index=True→ reindex result.join→ how to handle mismatched columns ('outer','inner').
Example
import pandas as pd
df1 = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
df2 = pd.DataFrame({"A": [5, 6], "B": [7, 8]})
pd.concat([df1, df2], ignore_index=True)
Copy to Clipboard
Output:
A B
0 1 3
1 2 4
2 5 7
3 6 8
Copy to Clipboard
Merge (pd.merge)
This is like SQL joins (inner, left, right, outer). It combines DataFrames based on common keys/columns.
Syntax:
pd.merge(left, right, how="inner", on=None, left_on=None, right_on=None)
Copy to Clipboard
Key Parameters
on→ column(s) to join on (must exist in both).left_on,right_on→ join on different column names.how→ type of join:"inner"→ only matching rows"left"→ keep all from left"right"→ keep all from right"outer"→ union of keys
Example:
df1 = pd.DataFrame({"ID": [1, 2, 3], "Name": ["Alice", "Bob", "Charlie"]})
df2 = pd.DataFrame({"ID": [2, 3, 4], "Salary": [50000, 60000, 70000]})
pd.merge(df1, df2, on="ID", how="inner")
Copy to Clipboard
Output:
ID Name Salary
0 2 Bob 50000
1 3 Charlie 60000
Copy to Clipboard
Join (df.join)
A convenience method for combining DataFrames by index (or by a key column).
Syntax:
df1.join(df2, how="left", on=None, lsuffix="", rsuffix="")
Copy to Clipboard
Key Parameters
on→ column in calling DataFrame to use as join key (if not index).lsuffix,rsuffix→ handle overlapping column names.how→ join type (same as merge).
Example:
df1 = pd.DataFrame({"Name": ["Alice", "Bob"], "ID": [1, 2]}).set_index("ID")
df2 = pd.DataFrame({"Salary": [50000, 60000]}, index=[1, 2])
df1.join(df2)
Copy to Clipboard
Output:
Name Salary
ID
1 Alice 50000
2 Bob 60000
Copy to Clipboard














