
- 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
Python - Fetch columns between two Pandas DataFrames by Intersection
To fetch columns between two DataFrames by Intersection, use the intersection() method. Let us create two DataFrames −
# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], }) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Units_Sold": [ 100, 110, 150, 80, 200, 90] })
Fetch common columns −
dataFrame2.columns.intersection(dataFrame1.columns)
Example
Following is the complete code −
import pandas as pd # creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], }) print"Dataframe1...\n",dataFrame1 # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Units_Sold": [ 100, 110, 150, 80, 200, 90] }) print"Dataframe2...\n",dataFrame2 # getting common columns using intersection() res = dataFrame2.columns.intersection(dataFrame1.columns) print"\nCommon columns...\n",res
Output
This will produce the following output −
Dataframe1... Car Cubic_Capacity Reg_Price 0 Bentley 2000 7000 1 Lexus 1800 1500 2 Tesla 1500 5000 3 Mustang 2500 8000 4 Mercedes 2200 9000 5 Jaguar 3000 6000 Dataframe2... Car Units_Sold 0 BMW 100 1 Lexus 110 2 Tesla 150 3 Mustang 80 4 Mercedes 200 5 Jaguar 90 Common columns... Index([u'Car'], dtype='object')
- Related Articles
- Python Pandas – Fetch the Common rows between two DataFrames with concat()
- Python – Get the Columns Shared by Two Pandas DataFrames using Numpy
- Python Pandas – Can we use & Operator to find common columns between two DataFrames?
- Python Pandas – Find the Difference between two Dataframes
- Python Pandas - Finding the uncommon rows between two DataFrames
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- Python Pandas – Find the common rows between two DataFrames with merge()
- Python Pandas – Check if two Dataframes are exactly same
- Python - How to Concatenate more than two Pandas DataFrames?
- Python Pandas - Form the intersection of two Index objects
- How to append two DataFrames in Pandas?
- How to compare two DataFrames in Python Pandas with missing values
- Python - How to Concatenate Two or More Pandas DataFrames along rows?
- Python - Concatenate Pandas DataFrames Without Duplicates
- Correlation between two numeric columns in a Pandas DataFrame

Advertisements