pandas
dataframes
python
data analysis
filtering

pandas: get rows that are not in another DataFrame

Codemia Knowledge Hub ·

Finding rows in one DataFrame that are not in another is an anti-join. The most reliable general approach in pandas is to merge on the columns that define row identity, with indicator=True, and keep only the rows marked left_only.

Anti-join with merge(indicator=True)

python
1import pandas as pd
2
3left = pd.DataFrame({
4    "id": [1, 2, 3, 4],
5    "name": ["Ada", "Bob", "Cara", "Dan"],
6})
7
8right = pd.DataFrame({
9    "id": [2, 4],
10    "name": ["Bob", "Dan"],
11})
12
13result = (
14    left.merge(right, how="left", indicator=True)
15        .query('_merge == "left_only"')
16        .drop(columns="_merge")
17)
18
19print(result)

Output:

text
   id  name
0   1   Ada
2   3  Cara

indicator=True adds a _merge column saying whether each row matched the left frame only, the right frame only, or both.

Comparing on key columns only

Often you want equality on a subset of columns, not the full row:

python
1result = (
2    left.merge(right[["id"]], on="id", how="left", indicator=True)
3        .query('_merge == "left_only"')
4        .drop(columns="_merge")
5)

Now a row counts as present in the other frame based only on id, whatever the other columns contain.

MultiIndex membership test

To compare full rows with index operations, turn the relevant columns into a MultiIndex:

python
1mask = ~left.set_index(["id", "name"]).index.isin(
2    right.set_index(["id", "name"]).index
3)
4
5result = left[mask]
6print(result)

This is a pure membership test that does not bring extra columns along from a merge, though the merge approach is usually easier to read and extend.

When a single-column isin is enough

A common first attempt is:

python
left[~left["id"].isin(right["id"])]

This works only when one key column truly defines the comparison. When several columns define identity, use merge or the index-based comparison.

Duplicates and missing values

If left has repeated rows and right has one matching row, decide whether you want all matching duplicates removed or only some. The merge approach usually removes every left row whose join keys appear in right. That is often the right business interpretation, but not always.

Missing values in key columns also affect the result. Define the intended behavior before comparing; cleaning or filling key columns first is usually safer than relying on implicit missing-value comparison rules. This matters most when missing keys carry meaning, such as "unknown user" versus "invalid row".

The most common mistake overall is not deciding which columns define "the same row". Full-row difference and key-based difference are different operations, and that choice usually decides whether the result is correct or subtly wrong.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design