0

For example, the data is:

a=pd.DataFrame({'aa':[1,2,3]})
b=pd.DataFrame({'bb':[4,5]})

what I want is to union these two data frames so the new frame is :

aa    bb
1      4
1      5
2      4
2      5
3      4
3      5

You can see that every value in a is linked to all the values in b in the new frame. I probably can use tile or repeat to do this. But I have multiple frames which need to be done repeatedly. So I want to know if there is a better way?

Could anyone help me out here?

Feng Chen
  • 2,139
  • 4
  • 33
  • 62

2 Answers2

2

You can do it like this:

In [24]: a['key'] = 1

In [25]: b['key'] = 1

In [27]: pd.merge(a, b, on='key').drop('key', axis=1)
Out[27]: 
   aa  bb
0   1   4
1   1   5
2   2   4
3   2   5
4   3   4
5   3   5
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
1

you can use pd.MultiIndex.from_product and then reset_index. It is generating all the combinations between both set of data (the same idea than itertools.product)

df_outut = (pd.DataFrame(index=pd.MultiIndex.from_product([a.aa,b.bb],names=['aa','bb']))
              .reset_index())

and you get

   aa  bb
0   1   4
1   1   5
2   2   4
3   2   5
4   3   4
5   3   5
Ben.T
  • 29,160
  • 6
  • 32
  • 54