
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Merge Python Pandas dataframe with a common column and set NaN for unmatched values
To merge two Pandas DataFrame with common column, use the merge() function and set the ON parameter as the column name. To set NaN for unmatched values, use the “how” parameter and set it left or right. That would mean, merging left or right.
At first, let us import the pandas library with an alias −
import pandas as pd
Let us create DataFrame1 −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
Let us create DataFrame2 −
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )
Now, merge DataFrames with common column Car. The left" “displays all the values of the left DataFrame and sets NaN for unmatched values from 2nd DataFrame −
mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car', how ="left")
Example
Following is the 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', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } ) print"\nDataFrame2 ...\n",dataFrame2 # merge DataFrames with common column Car and "left" sets NaN for unmatched values from second DataFrame mergedRes = pd.merge(dataFrame1, dataFrame2, on ='Car', how ="left") print"\nMerged data frame with common column...\n", mergedRes
Output
Following is the code −
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 Tesla 5000 3 Mustang 8000 4 Mercedes 9000 5 Jaguar 6000 Merged data frame with common column... Car Units Reg_Price 0 BMW 100 7000.0 1 Lexus 150 1500.0 2 Audi 110 NaN 3 Mustang 80 8000.0 4 Bentley 110 NaN 5 Jaguar 90 6000.0
- Related Articles
- Merge Pandas dataframe with a common column and set NaN for unmatched values
- Merge Pandas DataFrame with a common column
- How to count the NaN values in a column in a Python Pandas DataFrame?
- Python - Merge Pandas DataFrame with Outer Join
- Python - Merge Pandas DataFrame with Inner Join
- Python Pandas - Merge DataFrame with indicator value
- How to replace NaN values by Zeroes in a column of a Pandas DataFrame?
- Python – Merge two Pandas DataFrame
- Python - Merge Pandas DataFrame with Right Outer Join
- Python - Merge Pandas DataFrame with Left Outer Join
- Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
- Python Pandas - Replace all NaN elements in a DataFrame with 0s
- 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

Advertisements