
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python – Get the Columns Shared by Two Pandas DataFrames using Numpy
To get the columns shared by two DataFrames, use the intersect1d() method. This method is provided by numpy, so you need to import Numpy also with Pandas. Let us first import the required libraries −
import pandas as pd import numpy as np
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],"Units_Sold": [ 100, 110, 150, 80, 200, 90] }) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Units_Sold": [ 100, 110, 150, 80, 200, 90] })
Get common columns using the numpy method intersect1d() −
res = np.intersect1d(dataFrame2.columns, dataFrame1.columns)
Example
Following is the code −
import pandas as pd import numpy as np # 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],"Units_Sold": [ 100, 110, 150, 80, 200, 90] }) 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 # get common columns using intersect1d() res = np.intersect1d(dataFrame2.columns, dataFrame1.columns) print"\nCommon columns...\n",res
Output
This will produce the following output −
Dataframe1... Car Cubic_Capacity Reg_Price Units_Sold 0 Bentley 2000 7000 100 1 Lexus 1800 1500 110 2 Tesla 1500 5000 150 3 Mustang 2500 8000 80 4 Mercedes 2200 9000 200 5 Jaguar 3000 6000 90 Dataframe2... Car Units_Sold 0 BMW 100 1 Lexus 110 2 Tesla 150 3 Mustang 80 4 Mercedes 200 5 Jaguar 90 Common columns... ['Car' 'Units_Sold']
- Related Articles
- Python - Fetch columns between two Pandas DataFrames by Intersection
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- 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 Pandas – Check if two Dataframes are exactly same
- Python - How to Concatenate more than two Pandas DataFrames?
- Python Pandas – Fetch the Common rows between two DataFrames with concat()
- Python Pandas – Find the common rows between two DataFrames with merge()
- Plotting multiple dataframes using Pandas functionality
- 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
- How to get the correlation between two columns in Pandas?

Advertisements