Sometimes, we want to compare two DataFrames and output their differences side-by-side with Python Pandas.
In this article, we’ll look at how to compare two DataFrames and output their differences side-by-side with Python Pandas.
How to compare two DataFrames and output their differences side-by-side with Python Pandas?
To compare two DataFrames and output their differences side-by-side with Python Pandas, we can use the data frame’s compare
method.
For instance, we write:
import pandas as pd
df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df2 = pd.DataFrame({'a': [1, 2], 'b': [3, 5]})
changed = df1.compare(df2)
print(changed)
We create 2 data frames df1
and df2
.
Then we call df1.compare
with df2
to compare the difference between df1
and df2
.
Therefore, changed
is:
b
self other
1 4.0 5.0
Conclusion
To compare two DataFrames and output their differences side-by-side with Python Pandas, we can use the data frame’s compare
method.