Given a dataframe:
id value
0 1 a
1 2 b
2 3 c
I want to get a new dataframe that is basically the cartesian produ
This can be done entirely in pandas:
df.loc[:, 'key_col'] = 1 # create a join column that will give us the Cartesian Product
(df.merge(df, df, on='key_col', suffixes=('', '_2'))
.query('id != id_2') # filter out joins on the same row
.drop('key_col', axis=1)
.reset_index(drop=True))
Or if you don't want to have to drop the dummy column, you can temporarily create it when calling df.merge:
(df.merge(df, on=df.assign(key_col=1)['key_col'], suffixes=('', '_2'))
.query('id != id_2') # filter out joins on the same row
.reset_index(drop=True))