How to Calculate Cross Join Between Two DataFrames in Pandas

Preet Sanghavi Feb 02, 2024
How to Calculate Cross Join Between Two DataFrames in Pandas

In the following tutorial, we will discuss how to perform a cross join between two Pandas data frames.

Steps to Calculate Cross Join Between Two DataFrames in Pandas

The following are the steps to calculate cross join between two data frames in Pandas.

Import Pandas

We will import the Pandas library to perform the cross join to get started.

import pandas as pd

Create Pandas DataFrames

We will now create two sample data frames to perform the cross join operation. The two data frames will contain the alphabets and numbers, respectively.

data1 = {"A": ["a", "b"]}
data2 = {"B": [1, 2, 3]}
df = pd.DataFrame(data1, index=[0, 1])
df1 = pd.DataFrame(data2, index=[2, 3, 4])

Calculate Cross Join Between Two DataFrames in Pandas

To perform the cross join between the two created sample data frames, we will need to create a key column in both the data frames to merge on the same key column.

df["key"] = 2
df1["key"] = 2

We will merge both the data frames on the new key column and drop the key column to perform the cross join.

res = pd.merge(df, df1, on="key").drop("key", axis=1)

Now, print the res variable to see the cross join results between our two data frames.

Output:

   A  B
0  a  1
1  a  2
2  a  3
3  b  1
4  b  2
5  b  3

We can see the cross join between our two sample data frames in the output. Thus, we can successfully calculate cross join between two data frames in Pandas using the above technique.

Preet Sanghavi avatar Preet Sanghavi avatar

Preet writes his thoughts about programming in a simplified manner to help others learn better. With thorough research, his articles offer descriptive and easy to understand solutions.

LinkedIn GitHub

Related Article - Pandas DataFrame