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


Preparation

Before any data manipulation can occur, one (1) new library will require installation:

To install this library, 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.

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


Feel free to view the PyCharm installation guide for the required library.


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

💡 Note: To follow along with the examples below, click here to download the finxters.csv file of auto-generated dummy user data. Move this file to the current working directory.


The equals() method compares two (2) DataFrames/Series against each other to determine if they have an identical shape and elements. If identical return True, otherwise return False.

The syntax for this method is as follows:

DataFrame.equals(other)

Parameter Description
other A DataFrame or Series to compare.

For this example, we have two (2) DataFrames containing grades for three (3) students.

df_scores1 = pd.DataFrame({'Micah': [91, 58, 73], 'Bob': [53, 87, 46], 'Chloe': [60, 54, 61]})

df_scores2 = pd.DataFrame({'Micah': [91, 58, 73], 'Bob': [53, 87, 46], 'Chloe': [60, 54, 61]})

result = df_scores1.equals(df_scores2) print(result) print(df_scores1.shape) print(df_scores2.shape)

Output

True (3, 3) (3, 3)


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.