Categories
Python Answers

How to compare two dataframes and getting the differences with Python?

Spread the love

Sometimes, we want to compare two dataframes and getting the differences with Python.

In this article, we’ll look at how to compare two dataframes and getting the differences with Python.

How to compare two dataframes and getting the differences with Python?

To compare two dataframes and getting the differences with Python, we can use the concat and drop_duplicates methods.

For instance, we write

df_diff = pd.concat([df1,df2]).drop_duplicates(keep=False)

to concatenate the df1 and df2 data frames together with concat.

And then we call drop_duplicates with keep set to False to return a new data frame that has the entries in df1 and df2 but with the duplicate values dropped.

Conclusion

To compare two dataframes and getting the differences with Python, we can use the concat and drop_duplicates methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *