

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 – Find the common rows between two Data Frames
To find the common rows between two DataFrames, use the merge() method. Let us first create DataFrame1 with two columns −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
Create DataFrame2 with two columns −
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 250, 150, 80, 130, 90] } )
To find the common rows −
dataFrame1.merge(dataFrame2, how = 'inner' ,indicator=False)
Example
Following is the code −
import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', '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, 250, 150, 80, 130, 90] } ) print"\nDataFrame2 ...\n",dataFrame2 # check for equality print"\nAre both the DataFrames equal? ",dataFrame1.equals(dataFrame2) # finding common rows between two DataFrames resData = dataFrame1.merge(dataFrame2, how = 'inner' ,indicator=False) print"\nCommon rows between two DataFrames...\n",resData
Output
This will produce the following output −
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Tesla 80 4 Bentley 110 5 Jaguar 90 DataFrame2 ... Car Units 0 BMW 100 1 Lexus 250 2 Audi 150 3 Mustang 80 4 Bentley 130 5 Jaguar 90 Are both the DataFrames equal? False Common rows between two DataFrames... Car Units 0 BMW 100 1 Jaguar 90
- Related Questions & Answers
- Python Pandas – Find the common rows between two DataFrames with merge()
- Python Pandas – Fetch the Common rows between two DataFrames with concat()
- How to find the correlation coefficient between rows of two data frames in R?
- Python Pandas - Finding the uncommon rows between two DataFrames
- How to find the correlation coefficient between two data frames in R?
- Python Pandas – Find the Difference between two Dataframes
- Select DataFrame rows between two index values in Python Pandas
- Python Pandas – Can we use & Operator to find common columns between two DataFrames?
- Fetch data between two rows in MySQL?
- How to create the combination of rows in two data frames having same columns in R?
- Find the common elements between two columns of an R dataframe.
- Filter the rows – Python Pandas
- How to switch between two frames in Tkinter?
- How to find the common elements between two or more arrays in JavaScript?
- How to combine multiple R data frames that contains one common column?
Advertisements