Home >Backend Development >Python Tutorial >How to Identify Rows Present in One Pandas DataFrame but Absent in Another?

How to Identify Rows Present in One Pandas DataFrame but Absent in Another?

DDD
DDDOriginal
2024-12-14 08:20:15398browse

How to Identify Rows Present in One Pandas DataFrame but Absent in Another?

Identifying Rows Disparate between DataFrames

Problem Statement

Given two Pandas dataframes (df1 and df2) with intersecting rows, the task is to isolate the rows in df1 that are absent in df2.

Solution

To solve this problem, we can perform a left-join from df1 to df2, ensuring we eliminate duplicates in df2 to ensure each row of df1 joins with only one row of df2.

This left-join creates an extra column _merge indicating the origin of each row.

To filter for rows exclusive to df1, we apply a boolean condition:

Incorrect Solutions

Some solutions err in checking each value in each column independently rather than considering row-wise presence. For instance, this solution:

returns an incorrect result because it fails to capture the row with values [3, 10], which is absent in common:

The above is the detailed content of How to Identify Rows Present in One Pandas DataFrame but Absent in Another?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn