Anti-Join & Semi-Join

Python · Combining DataFrames (SQL Joins & Unions)

Theory

pandas has no how='anti' or how='semi', but both are short patterns. A semi-join keeps rows of the left table that have a match on the right, without adding any right-hand columns and without duplicating rows: customers[customers['cust_id'].isin(orders['cust_id'])] lists customers who have ordered at least once, each once, even if they ordered many times. This is SQL's WHERE EXISTS or IN (subquery).

An anti-join keeps left rows with no match: customers[~customers['cust_id'].isin(orders['cust_id'])] gives customers who never ordered - SQL's NOT EXISTS or LEFT JOIN ... WHERE o.cust_id IS NULL. For multi-column keys, use a left merge with indicator=True and keep rows where _merge == 'left_only', after taking only the key columns from the right and dropping their duplicates so nothing is multiplied.

Why we use it

'Which customers never ordered?', 'which products have no sales?', 'which records are missing from the target system?' are everyday questions. Anti- and semi-joins answer them without inflating rows or adding unwanted columns.

When to use it

Use isin for single-column keys; it is short and fast. Use merge with indicator=True for multi-column keys. If you actually need columns from the right table, use a normal merge instead of a semi-join.

How it works

  1. Need rows of A based on B
  2. Do you need columns from B? Yes: Use a normal merge. No: Filter A only.
  3. Keep rows that HAVE a match? Yes: Semi: A[A.key.isin(B.key)]. No: Anti: A[~A.key.isin(B.key)].
  4. For multi-column keys use merge(indicator=True)
  5. Filtered rows of A, each once

Syntax

semi: left[left['key'].isin(right['key'])]
anti: left[~left['key'].isin(right['key'])]
anti (multi-key):
  m = left.merge(right[keys].drop_duplicates(), on=keys, how='left', indicator=True)
  m[m['_merge'] == 'left_only'].drop(columns='_merge')

Example

never_ordered = customers[~customers['cust_id'].isin(orders['cust_id'])]

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