- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas – Check if any specific column of two DataFrames are equal or not
To check if any specific column of two DataFrames are equal or not, use the equals() method. Let us first create DataFrame1 with two columns −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
Create DataFrame2 with two columns −
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Mercedes', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
Check for the equality of a specific column Units −
dataFrame2['Units'].equals(dataFrame1['Units'])
Example
Following is the code −
import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"DataFrame1 ...\n",dataFrame1 # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"\nDataFrame2 ...\n",dataFrame2 # check for equality print"\nAre both the DataFrame objects equal? ",dataFrame1.equals(dataFrame2) # check for specific column Units equality print"\nAre both the DataFrames have similar Units column? ",dataFrame2['Units'].equals(dataFrame1['Units'] )
Output
This will produce the following output −
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 DataFrame2 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 Are both the DataFrame objects equal? True Are both the DataFrames have similar Units column? True
- Related Articles
- Python Pandas – Check if two Dataframes are exactly same
- Python Pandas - Check if the dataframe objects are equal or not
- Check if two strings are equal or not in Arduino
- Python Pandas - Determine if two Index objects with opposite orders are equal or not
- How to check if some specific columns of an R data frame are equal to a column or not?
- Check if two StringDictionary objects are equal or not in C#
- Golang Program to Check If Two Arrays are Equal or Not
- Swift Program to Check if Two Arrays Are Equal or Not
- Check whether two schedules are view equal or not(DBMS)
- Python Pandas - Determine if two Index objects are equal
- Check if all levels of two trees are anagrams or not in Python
- Python - How to Concatenate Two or More Pandas DataFrames along rows?
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check Whether Two Matrices Are Equal or Not
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n

Advertisements