
- 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 Pandas – Merge and create cartesian product from both the DataFrames
To merge Pandas DataFrame, use the merge() function. The cartesian product is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −
how = “cross”
At first, let us import the pandas library with an alias −
import pandas as pd
Create DataFrame1 −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 120] } )
Create DataFrame2
dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Tesla', 'Jaguar'],"Reg_Price": [7000, 8000, 9000] } )
Next, merge DataFrames with "cross" in "how" parameter i.e. Cartesian Product −
mergedRes = pd.merge(dataFrame1, dataFrame2, how ="cross")
Example
Following is the code
import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Mustang', 'Bentley', 'Jaguar'],"Units": [100, 150, 110, 120] } ) print("DataFrame1 ...\n",dataFrame1) # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Tesla', 'Jaguar'],"Reg_Price": [7000, 8000, 9000] } ) print("\nDataFrame2 ...\n",dataFrame2) # merge DataFrames with "cross" in "how" parameter i.e Cartesian Product mergedRes = pd.merge(dataFrame1, dataFrame2, how ="cross") print("\nMerged dataframe with cartesian product...\n", mergedRes)
Output
This will produce the following output −
DataFrame1 ... Car Units 0 BMW 100 1 Mustang 150 2 Bentley 110 3 Jaguar 120 DataFrame2 ... Car Reg_Price 0 BMW 7000 1 Tesla 8000 2 Jaguar 9000 Merged dataframe with cartesian product... Car Units Car_y Reg_Price 0 BMW 100 BMW 7000 1 BMW 100 Tesla 8000 2 BMW 180 Jaguar 9000 3 Mustang 150 BMW 7000 4 Mustang 150 Tesla 8000 5 Mustang 150 Jaguar 9000 6 Bentley 110 BMW 7000 7 Bentley 110 Tesla 8000 8 Bentley 110 Jaguar 9000 9 Jaguar 120 BMW 7000 10 Jaguar 120 Tesla 8000 11 Jaguar 120 Jaguar 9000
- Related Articles
- Merge, Join and Concatenate DataFrames using Pandas
- Python Pandas – Find the common rows between two DataFrames with merge()
- Python - Merge DataFrames of different length
- Python - Concatenate Pandas DataFrames Without Duplicates
- Python Pandas – Find the Difference between two Dataframes
- Making matplotlib scatter plots from dataframes in Python's pandas
- Python Pandas - Create a Series with both the original index and name
- Python Pandas - Create a DataFrame with both the original index and name
- Python Pandas - Finding the uncommon rows between two DataFrames
- Python – Merge two Pandas DataFrame
- Python Pandas – Check if two Dataframes are exactly same
- Python - Fetch columns between two Pandas DataFrames by Intersection
- Python - How to Concatenate more than two Pandas DataFrames?
- Python Pandas - Create Multiindex from arrays
- Python Pandas - Create Multiindex from dataframe

Advertisements