1

I have two dataframes that I would like to combine combinatorial-wise (i.e. combinatorially join each row from one df to each row of another df). I can do this by merging on 'key's but my solution is clearly cumbersome. I'm looking for a more straightforward, even pythonesque way of handling this operation. Any suggestions?

MWE:

fred = pd.DataFrame({'A':[1., 4.],'B':[2., 5.], 'C':[3., 6.]})
print(fred)
    A    B    C
0  1.0  2.0  3.0
1  4.0  5.0  6.0

jim = pd.DataFrame({'one':['a', 'c'],'two':['b', 'd']})
print(jim)
   one two
0   a   b
1   c   d


fred['key'] = [1,2]
jim1 = jim.copy()
jim1['key'] = 1
jim2 = jim.copy()
jim2['key'] = 2
jim3 = jim1.append(jim2)

jack = pd.merge(fred, jim3, on='key').drop(['key'], axis=1)
print(jack)
    A    B    C   one two
0  1.0  2.0  3.0   a   b
1  1.0  2.0  3.0   c   d
2  4.0  5.0  6.0   a   b
3  4.0  5.0  6.0   c   d
FredDref
  • 13
  • 2

2 Answers2

1

You can join every row of fred with every row of jim by merging on a key column which is equal to the same value (say, 1) for every row:

In [16]: pd.merge(fred.assign(key=1), jim.assign(key=1), on='key').drop('key', axis=1)
Out[16]: 
     A    B    C one two
0  1.0  2.0  3.0   a   b
1  1.0  2.0  3.0   c   d
2  4.0  5.0  6.0   a   b
3  4.0  5.0  6.0   c   d
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

Are you looking for the cartesian product of the two dataframes, like a cross join?

It is answered here.

herculanodavi
  • 228
  • 2
  • 12