merge()를 사용하여 두 DataFrame 간의 공통 행을 찾으려면 "how 매개변수를 사용합니다. "로 "내부 "이것이 SQL 내부 조인처럼 작동하고 이것이 우리가 달성하고자 하는 것이기 때문입니다.
두 개의 열이 있는 DataFrame1을 생성해 보겠습니다. -
dataFrame1 = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
"Reg_Price": [1000, 1500, 1100, 800, 1100, 900] }
) 두 개의 열이 있는 DataFrame2 생성 -
dataFrame2 = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
"Reg_Price": [1200, 1500, 1000, 800, 1100, 1000]
}
) 이제 공통 행을 찾아봅시다 -
dataFrame1.merge(dataFrame2, how = 'inner' ,indicator=False)
예
다음은 코드입니다 -
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
출력
이것은 다음과 같은 출력을 생성합니다 -
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