1

Say I have two dataframes, and they look like this:

1/31
2/28
3/31

and:

black    dog
white    dog
orange   cat

I want my resulting dataframe to be as such:

1/31   black   dog
1/31   white   dog
1/31   orange  cat
2/28   black   dog
2/28   white   dog
2/28   orange  cat
3/31   black   dog
3/31   white   dog
3/31   orange  cat

Is there an easy matrix multiplication to multiply these two panda dataframes together? I am unfamiliar with the syntax to do so, and if I can I want to avoid merge/forloops if possible.

user7180132
  • 321
  • 2
  • 4
  • 15

2 Answers2

2

One way to do this is to use pd.merge:

df1.assign(key=1).merge(df2.assign(key=1), on='key').drop('key', axis=1)

Output:

    0_x     0_y    1
0  1/31   black  dog
1  1/31   white  dog
2  1/31  orange  cat
3  2/28   black  dog
4  2/28   white  dog
5  2/28  orange  cat
6  3/31   black  dog
7  3/31   white  dog
8  3/31  orange  cat
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
0

Intentionally obnoxious, but surprisingly fast:

from itertools import starmap as smp, product as xp
pd.concat(
    smp(
        lambda x, i: x.iloc[i].reset_index(drop=True),
        (lambda y: zip(y, map(list, zip(*xp(*map(range, map(len, y)))))))([d1, d2])
    ), axis=1
)

   col1   color animal
0  1/31   black    dog
1  1/31   white    dog
2  1/31  orange    cat
3  2/28   black    dog
4  2/28   white    dog
5  2/28  orange    cat
6  3/31   black    dog
7  3/31   white    dog
8  3/31  orange    cat

Setup

d1 = pd.DataFrame(dict(col1=['1/31', '2/28', '3/31']))

d2 = pd.DataFrame(OrderedDict(
    color=['black', 'white', 'orange'],
    animal=['dog', 'dog', 'cat']
))
piRSquared
  • 285,575
  • 57
  • 475
  • 624