How to Perform a Cross Join in Pandas (With Example)


You can use the following basic syntax to perform a cross join in pandas:

#create common key
df1['key'] = 0
df2['key'] = 0

#outer merge on common key (e.g. a cross join)
df1.merge(df2, on='key', how='outer')

The following example shows how to use this function in practice.

Example: Perform Cross Join in Pandas

Suppose we have the following two pandas DataFrames:

import pandas as pd

#create first DataFrame
df1 = pd.DataFrame({'team': ['A', 'B', 'C', 'D'],
                    'points': [18, 22, 19, 14]})

print(df1)

  team  points
0    A      18
1    B      22
2    C      19
3    D      14

#create second  DataFrame
df2 = pd.DataFrame({'team': ['A', 'B', 'F'],
                    'assists': [4, 9, 8]})

print(df2)

  team  assists
0    A        4
1    B        9
2    F        8

The following code shows how to perform a cross join on the two DataFrames:

#create common key
df1['key'] = 0
df2['key'] = 0

#perform cross join
df3 = df1.merge(df2, on='key', how='outer')

#drop key columm
del df3['key']

#view results
print(df3)

   team_x  points team_y  assists
0       A      18      A        4
1       A      18      B        9
2       A      18      F        8
3       B      22      A        4
4       B      22      B        9
5       B      22      F        8
6       C      19      A        4
7       C      19      B        9
8       C      19      F        8
9       D      14      A        4
10      D      14      B        9
11      D      14      F        8

The result is one DataFrame that contains every possible combination of rows from each DataFrame.

For example, the first row of the first DataFrame contains team A and 18 points. This row is matched with every single row in the second DataFrame.

Next, the second row of the first DataFrame contains team B and 22 points. This row is also matched with every single row in the second DataFrame.

The end result is a DataFrame with 12 rows.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Do a Left Join in Pandas
How to Do a Left Join in Pandas
Pandas Join vs. Merge: What’s the Difference?

Leave a Reply

Your email address will not be published.