Python Pandas - Check if the dataframe objects are equal or not



To check if the DataFrame objects are equal, use the equals() method. At first, let us create DataFrame1 with two columns −

dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )

Create DataFrame2 with two columns 

dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )

To check if the DataFrame objects are equals, use the equals() method

dataFrame1.equals(dataFrame2)

Example

Following is the code

import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } ) print"DataFrame1 ...\n",dataFrame1 # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } ) print"\nDataFrame2 ...\n",dataFrame2 # check for equality print"\nAre both the DataFrame objects equal? ",dataFrame1.equals(dataFrame2)

Output

This will produce the following output

DataFrame1 ...        Car   Reg_Price 0      BMW        7000 1    Lexus        1500 2     Audi        5000 3  Mustang        8000 4  Bentley        9000 5   Jaguar        6000 DataFrame2 ...        Car Reg_Price 0      BMW 7000 1    Lexus 1500 2     Audi 5000 3  Mustang 8000 4  Bentley 9000 5   Jaguar 6000 Are both the DataFrame objects equal? True
Updated on: 2021-09-14T14:19:50+05:30

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements