Join on Index

Python · Combining DataFrames (SQL Joins & Unions)

Theory

Sometimes the key is the row index rather than a column - for example after set_index('cust_id') or after a groupby. left.join(right) is a shortcut that matches left's index (or a column given with on=) against right's index, with how='left' by default. If both tables share column names, join requires lsuffix= / rsuffix=. You can join several frames at once: a.join([b, c]).

merge can do the same with left_index=True and right_index=True, or mix them: left.merge(right, left_on='cust_id', right_index=True) matches a column on one side to the index on the other. A typical use is attaching a groupby summary back to a lookup table: totals = orders.groupby('cust_id')['amount'].sum() has cust_id as its index, so customers.set_index('cust_id').join(totals) adds each customer's total, with NaN for customers who never ordered.

Why we use it

Indexes are pandas' built-in keys. Results of groupby, pivot_table and set_index are already indexed, so joining on the index avoids resetting and renaming columns and keeps code short.

When to use it

Use join when the right table's key is its index, especially for adding aggregated Series or lookup tables. Use merge when keys are ordinary columns or you need validate/indicator; reset_index() first if you prefer columns.

How it works

  1. customers.join(totals)
  2. Read the index labels of both tables
  3. Left label in the right index? Yes: Attach the right values. No: Right columns become NaN.
  4. Keep left order (how='left')
  5. Left table with extra columns from the right

Syntax

left.join(right, how='left', lsuffix='', rsuffix='')
left.join(right, on='key_col')
left.merge(right, left_index=True, right_index=True)
left.merge(right, left_on='key', right_index=True)

Example

totals = orders.groupby('cust_id')['amount'].sum().rename('total')
print(customers.set_index('cust_id').join(totals))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic