Concatenate, Merge & Join

Pandas Basics

1 min read

Published Sep 29 2025, updated Aug 17 2026


21
0
0
0

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)

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 pddf1 = pd.DataFrame({"A": [1, 2], "B": [3, 4]})df2 = pd.DataFrame({"A": [5, 6], "B": [7, 8]})pd.concat([df1, df2], ignore_index=True)

Output:

   A  B0  1  31  2  42  5  73  6  8





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)

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")


Output:

   ID   Name  Salary0   2    Bob   500001   3 Charlie   60000





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="")

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)

Output:

    Name  SalaryID               1   Alice   500002     Bob   60000
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Pandas Basics | Concatenate, Merge & Join | SimpleSteps.guide