Home  >  Article  >  Backend Development  >  How to Find Rows Present in One Dataframe but Not in Another (Comparing df1 and df2)?

How to Find Rows Present in One Dataframe but Not in Another (Comparing df1 and df2)?

Susan Sarandon
Susan SarandonOriginal
2024-10-19 21:07:03577browse

How to Find Rows Present in One Dataframe but Not in Another (Comparing df1 and df2)?

Comparing Dataframes: Finding Rows Present in One but Not in the Other

Comparing dataframes to identify differences is crucial for data quality assurance and merging operations. In this case, we have two dataframes (df1 and df2) with a specific structure and need to determine the rows present in df2 but not in df1.

Initially, attempts to compare dataframes using df1 != df2 resulted in an error. This approach only works for dataframes with identical rows and columns. To find symmetric differences, we need a different approach.

One method involves concatenating the dataframes:

df = pd.concat([df1, df2])
df = df.reset_index(drop=True)

Then, grouping the concatenated dataframe by all columns:

df_gpby = df.groupby(list(df.columns))

Next, we identify the unique records by obtaining the index values where only one row exists:

idx = [x[0] for x in df_gpby.groups.values() if len(x) == 1]

Using these indices, we can filter the dataframe to obtain the desired result:

df.reindex(idx)

This approach provides the rows present in df2 but absent in df1 based on the comparison of the Date index and the Fruit column.

The above is the detailed content of How to Find Rows Present in One Dataframe but Not in Another (Comparing df1 and df2)?. 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