Merge & Inner Join

Python · Combining DataFrames (SQL Joins & Unions)

Theory

pd.merge(left, right, on='key') - or left.merge(right, on='key') - combines two DataFrames side by side by matching key values, exactly like a SQL JOIN. The default how='inner' keeps only rows whose key exists in both tables. If customer 1 has two orders, the customer row is repeated once per order; customers with no orders and orders with unknown customers are dropped.

When the key has different names, use left_on='cust_id', right_on='customer_id' (both columns are kept, so drop one afterwards). on=['cust_id', 'order_date'] joins on several columns. Columns that exist in both tables but are not keys get suffixes, _x and _y by default; suffixes=('_cust', '_ord') makes them readable. Key columns should have the same dtype on both sides - an int key will not match a text key like '1'.

Why we use it

Real data is spread across tables: customers in one, orders in another, products in a third. merge brings related facts together so you can answer questions such as 'revenue by customer city'.

When to use it

Use an inner merge when you only want rows that match on both sides, for example orders with a known customer. If you must keep unmatched rows from one side, use a left, right or outer join instead; to stack tables vertically, use concat.

How it works

  1. customers.merge(orders, on='cust_id')
  2. Take each key value in the left table
  3. Key found in the right table? Yes: One row per matching right row. No: Drop the row (inner).
  4. Add suffixes to overlapping non-key columns
  5. Joined DataFrame with columns from both tables

Syntax

pd.merge(left, right, on='key', how='inner')
left.merge(right, left_on='a', right_on='b', suffixes=('_l', '_r'))
left.merge(right, on=['k1', 'k2'])

Example

result = orders.merge(customers, on='cust_id', how='inner')
print(result)

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