Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Equivalent of SQL CROSS JOIN (Cartesian Product) [duplicate]

Let's say I had two tables:

Table1:

   col1  col2
      0     1
      2     3

Table2:

   col3  col4
      5     6
      7     8

In SQL, if I made the following statement:

Select *
From Table1, Table2;

I would expect to get back a table with all combinations from both tables:

col1 col2 col3 col4
   0    1    5    6
   0    1    7    8
   2    3    5    6
   2    3    7    8

Is there a way to do the same thing with two dataframes in pandas?

like image 422
Browning Gentry Avatar asked Jan 14 '18 23:01

Browning Gentry


People also ask

Do pandas have a cross join?

In Pandas, there are parameters to perform left, right, inner or outer merge and join on two DataFrames or Series. However there's no possibility as of now to perform a cross join to merge or join two methods using how="cross" parameter.

What is the alternative for cross join?

If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN. An alternative way of achieving the same result is to use column names separated by commas after SELECT and mentioning the table names involved, after a FROM clause.

Which join is equivalent to Cartesian product join?

A CROSS JOIN is a JOIN operation that produces the Cartesian product of two tables.

Is Cross join same as Cartesian product?

The CROSS JOIN is used to generate a paired combination of each row of the first table with each row of the second table. This join type is also known as cartesian join.


1 Answers

A standard idiom is using the merge on a dummy column.

df1.assign(foo=1).merge(df2.assign(foo=1)).drop('foo', 1)

   col1  col2  col3  col4
0     0     1     5     6
1     0     1     7     8
2     2     3     5     6
3     2     3     7     8
like image 70
cs95 Avatar answered Oct 07 '22 12:10

cs95