Home >Backend Development >Python Tutorial >How to Identify Rows Present in One Pandas DataFrame but Absent in Another?
Given two Pandas dataframes (df1 and df2) with intersecting rows, the task is to isolate the rows in df1 that are absent in df2.
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:
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!