- 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
Merge Pandas DataFrame with a common column
To merge two Pandas DataFrame with common column, use the merge() function and set the ON parameter as the column name.
At first, let us import the pandas library with an alias −
import pandas as pd
Let us create the 1st DataFrame −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 80, 110, 90] } )
Next, create the 2nd DataFrame −
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Mercedes', 'Jaguar'],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )
Now, merge the two DataFrames with a column column “Car” −
mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car')
Example
Following is the complete 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', 'Mercedes', 'Jaguar'],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } ) print"\nDataFrame2 ...\n",dataFrame2 # merge DataFrames with common column Car mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car') print"\nMerged data frame with common column...\n", mergedRes
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 Reg_Price 0 BMW 7000 1 Lexus 1500 2 Audi 5000 3 Mustang 8000 4 Mercedes 9000 5 Jaguar 6000 Merged data frame with common column... Car Units Reg_Price 0 BMW 100 7000 1 Lexus 150 1500 2 Audi 110 5000 3 Mustang 80 8000 4 Jaguar 90 6000
- Related Articles
- Merge Pandas dataframe with a common column and set NaN for unmatched values
- Merge Python Pandas dataframe with a common column and set NaN for unmatched values
- Python - Merge Pandas DataFrame with Outer Join
- Python - Merge Pandas DataFrame with Inner Join
- Python Pandas - Merge DataFrame with indicator value
- Python - Merge Pandas DataFrame with Right Outer Join
- Python - Merge Pandas DataFrame with Left Outer Join
- Python – Merge two Pandas DataFrame
- Python Pandas – Merge DataFrame with one-to-many relation
- Python Pandas – Merge DataFrame with many-to-one relation
- Python Pandas – Merge DataFrame with one-to-one relation
- Python Pandas – Find the common rows between two DataFrames with merge()
- Python - Add a new column with constant value to Pandas DataFrame
- How to Merge multiple CSV Files into a single Pandas dataframe ?
- Apply uppercase to a column in Pandas dataframe

Advertisements