I have two pandas dataframes:
from pandas import DataFrame
df1 = DataFrame({'col1':[1,2],'col2':[3,4]})
df2 = DataFrame({'col3':[5,6]})
What is the best practice to get their cartesian product (of course without writing it explicitly like me)?
#df1, df2 cartesian product
df_cartesian = DataFrame({'col1':[1,2,1,2],'col2':[3,4,3,4],'col3':[5,5,6,6]})
In recent versions of Pandas (>= 1.2) this is built into
mergeso you can do:This is equivalent to the previous pandas < 1.2 answer but is easier to read.
For pandas < 1.2:
If you have a key that is repeated for each row, then you can produce a cartesian product using merge (like you would in SQL).
Output:
See here for the documentation: http://pandas.pydata.org/pandas-docs/stable/merging.html