Pandas DataFrame compare() Method – Be on the Right Side of Change (original) (raw)


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


FeFeel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd import numpy


The compare() method compares two (2) DataFrames. This method returns the differences between them.

The syntax for this method is as follows:

DataFrame.compare(other, align_axis=1, keep_shape=False, keep_equal=False)

Parameter Description
other This parameter is the object (DataFrame) to use for comparison.
align_axis This parameter determines the axis to align the comparison. If zero (0) or index is selected, apply to each column. Default is 0 (column). If zero (1) or columns, apply to each row.
keep_shape If set to True, all column(s) stay. If False, only the ones with differing values remain.
keep_equal If set to True, keep equal values. If False, equal values display as NaN values.

For this example, we have two (2) DataFrames. One with existing customer login credentials and one with new customer credentials. This code compares the DataFrames and returns the results (the differences).

df_custs = pd.DataFrame({('jkende', 'Vzs*@4:kNq%)'), ('sarahJ', '{M$*3zB~-a-W'), ('AmyKerr', '*7#<bSt?Y_Z<')}, columns=['username', 'password'], index=['user-a', 'user-b', 'user-c']) print(df_custs)

df_new = pd.DataFrame({('jkende', 'Vzs*@4:kNq%)'), ('sarahJ', 'xc^O3&43P'), ('AmyKerr', '*7#<bSt?Y_Z<')}, columns=['username', 'password'], index=['user-a', 'user-b', 'user-c']) print(df_new)

result = df_custs.compare(df_new) print(result)

Output

df_custs

| | username | password | | | ---------- | -------- | -------------- | | user-a | AmyKerr | *7#<bSt?Y_Z< | | user-b | sarahJ | {M$*3zB~-a-W | | user-c | jkende | Vzs*@4:kNq%) |

df_new

| | username | password | | | ---------- | -------- | -------------- | | user-a | AmyKerr | *7#<bSt?Y_Z< | | user-b | sarahJ | xc^O3&43P | | user-c | jkende | Vzs*@4:kNq%) |

result

| | password | | | | ---------- | -------------- | --------- | | | self | other | | | user-b | {M$*3zB~-a-W | xc^O3&43P |

💡 Note: The user sarahJ resides in each DataFrame with different passwords.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.