
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python Pandas – Find the common rows between two DataFrames with merge()
To find the common rows between two DataFrames with merge(), use the parameter “how” as “inner” since it works like SQL Inner Join and this is what we want to achieve.
Let us create DataFrame1 with two columns −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Reg_Price": [1000, 1500, 1100, 800, 1100, 900] } )
Create DataFrame2 with two columns −
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Reg_Price": [1200, 1500, 1000, 800, 1100, 1000] } )
Let us now 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'], "Reg_Price": [1000, 1500, 1100, 800, 1100, 900] } ) print"DataFrame1 ...\n",dataFrame1 # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Reg_Price": [1200, 1500, 1000, 800, 1100, 1000] } ) print"\nDataFrame2 ...\n",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 Reg_Price 0 BMW 1000 1 Lexus 1500 2 Audi 1100 3 Tesla 800 4 Bentley 1100 5 Jaguar 900 DataFrame2 ... Car Reg_Price 0 BMW 1200 1 Lexus 1500 2 Audi 1000 3 Tesla 800 4 Bentley 1100 5 Jaguar 1000 Common rows between two DataFrames... Car Reg_Price 0 Lexus 1500 1 Tesla 800 2 Bentley 1100
- Related Articles
- Python Pandas – Fetch the Common rows between two DataFrames with concat()
- Python Pandas - Finding the uncommon rows between two DataFrames
- Python Pandas – Find the common rows between two Data Frames
- Python Pandas – Find the Difference between two Dataframes
- Python Pandas – Can we use & Operator to find common columns between two DataFrames?
- Python - How to Concatenate Two or More Pandas DataFrames along rows?
- Python - Fetch columns between two Pandas DataFrames by Intersection
- Python Pandas – Merge and create cartesian product from both the DataFrames
- Merge, Join and Concatenate DataFrames using Pandas
- Merge Pandas DataFrame with a common column
- Python – Merge two Pandas DataFrame
- How to compare two DataFrames in Python Pandas with missing values
- Python - Merge DataFrames of different length
- Python Pandas – Check if two Dataframes are exactly same
- Python - How to Concatenate more than two Pandas DataFrames?

Advertisements