Home >Backend Development >Python Tutorial >How to Calculate the Cartesian Product of Two Pandas DataFrames?
Problem:
Obtain the Cartesian product of two Pandas dataframes without explicitly defining it.
Example:
import pandas as pd df1 = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) df2 = pd.DataFrame({'col3': [5, 6]})
Desired output:
col1 col2 col3 0 1 3 5 1 1 3 6 2 2 4 5 3 2 4 6
Solution:
For Pandas >= 1.2:
Using the built-in merge function:
df1.merge(df2, how='cross')
For Pandas < 1.2:
Using merge with a repeated key:
key_df = pd.DataFrame({'key': [1, 1], 'col1': [1, 2], 'col2': [3, 4]}) merge(key_df, df2, on='key')[['col1', 'col2', 'col3']]
The above is the detailed content of How to Calculate the Cartesian Product of Two Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!