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.