Sling Academy
Home/Pandas/Mastering DataFrame.compare() method in Pandas (5 examples)

Mastering DataFrame.compare() method in Pandas (5 examples)

Last updated: February 22, 2024

Introduction

In the expansive world of data analysis, Pandas stands out as a pivotal library in Python for dealing with data structures and operations for manipulating numerical tables and time series. One of the essential techniques in data analysis is comparing datasets to understand differences or changes over time. This tutorial is dedicated to exploring the compare() method in Pandas through insightful examples, ranging from basic to advanced usage.

Before diving into the examples, ensure you have Pandas installed in your environment:

pip install pandas

Basic Comparison

Starting with the basics, let’s compare two DataFrames with the same structure:

import pandas as pd

# Sample DataFrames
df1 = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Paris', 'London']
})

df2 = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [26, 29, 35],
    'City': ['New York', 'Paris', 'Berlin']
})

# Comparison
comparison = df1.compare(df2)
print(comparison)

Output:

    Age            City       
    self other   self    other
0   NaN  26.0    NaN    NaN
1   NaN  29.0    Paris  Berlin

This basic example highlights how compare() method showcases differences. Non-matching values are displayed, while matching ones are omitted.

Handling Mismatches with the keep_shape Parameter

Next, let’s explore how to retain the original shape of our DataFrames when conducting comparisons, despite there being no differences in some rows:

comparison = df1.compare(df2, keep_shape=True)
print(comparison)

Output:

  Age            City         
  self other   self  other
0 NaN  26.0    NaN     NaN
1 NaN  29.0    Paris  Berlin
2 NaN  NaN     London Berlin

By setting keep_shape=True, rows with no differences are also included, with NaN values indicating no difference for easier readability and comparison.

Handling NA Values with the keep_equal Parameter

When comparing datasets, especially if they include missing values or you wish to highlight even the rows that are equal, the keep_equal parameter becomes useful:

comparison = df1.compare(df2, keep_equal=True)
print(comparison)

Output:

    Age             City         
    self other   self   other
0   25  26      New York New York
1   30  29      Paris    Berlin

This shows every row, indicating clearly where the differences and similarities lie, even for columns with no changes. This level of detail is valuable for comprehensive comparisons.

Advanced Comparison with Custom Output

For more complex comparisons, such as those involving data from different sources or formats, customization is key. Let’s consider another example where we want a more detailed comparison, possibly for a report:

result = df1.compare(df2, keep_shape=True, keep_equal=True)
result.columns = pd.MultiIndex.from_tuples([('Before', 'Age'), ('After', 'Age'), ('Before', 'City'), ('After', 'City')])
print(result)

Output:

  Before After   Before After 
    Age   Age      City   City
0  25     26     New York New York
1  30     29     Paris    Berlin
2  35     35     London   London

This approach customizes the output with meaningful column headings, offering a clearer, more digestible comparison for reports or presentations.

Utilizing compare() in Data Analysis Workflows

Finally, integrating compare() into data analysis workflows can significantly enhance data understanding and decision-making. Consider a scenario where monthly sales data are compared to identify trends, discrepancies, or errors:

# Assuming df_month1 and df_month2 are defined

comparison = df_month1.compare(df_month2)

# Further analysis based on comparison
...

This demonstrates how compare() can serve as a preliminary step in broader data analysis workflows, providing invaluable insights at a glance.

Conclusion

The compare() method in Pandas is an extraordinarily powerful tool for detecting differences between DataFrames. By mastering its usage through various parameters and customization, analysts can gain deeper insights into their data, facilitating more informed decision-making.

Next Article: Understanding DataFrame.join() method in Pandas (5 examples)

Previous Article: Pandas: How to convert a DataFrame to an xarray (4 examples)

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)