3.1 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Perform a Cross Join in Pandas (With Example)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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?

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories