1

I have two DataFrames which I want to combine completely, meaning for each row in A, all rows in B are joined. No matching on any index or column needs to be performed.

DataFrame A:

idx | A
  0 | foo
  1 | bar

DataFrame B:

idx | A
  0 | alpha
  1 | beta

Result in:

DataFrame AB

idxA idxB | A_left | A_right
   0    0 | foo    | alpha
        1 | foo    | beta
   1    0 | bar    | alpha
        1 | bar    | beta

Join and merge always expect a index or column to merge on. The cartesian product throws away the index, but I also want to combine the indices as in the example above.

Vincent
  • 46
  • 6
  • I've added two options for you. Take a look at both of them. At least one should be helpful. – cs95 Dec 24 '18 at 09:09
  • Did you try `df1.assign(key=1).merge(df2.assign(key=1), on='key').drop('key', 1)` That is the first option given in both questions. – cs95 Dec 24 '18 at 09:19
  • cartesian product was apparently the word I was looking for. Apparently there isn't a clean method. Thanks coldspeed ! – Vincent Dec 24 '18 at 09:22
  • But now the actual indices are not combined! How can I create a multi-index to match the original two indices? – Vincent Dec 24 '18 at 10:23
  • It's also hackish but you can reset the index before merging and set the index again: `df1.reset_index().assign(key=1).merge(df2.reset_index().assign(key=1), on='key').drop('key', axis=1).set_index(['idx_x', 'idx_y'])` – ayhan Dec 24 '18 at 10:43

0 Answers0