Computer >> Computer tutorials >  >> Programming >> Python

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