Skip to content Skip to sidebar Skip to footer

How To Concatenate Combinations Of Rows From Two Different Dataframes?

I have two dataframes with different column names. I want to create a new dataframe whose column names are the concatenation of the two dataframes columns. The resulting number of

Solution 1:

Use itertools.product():

import itertools
pd.DataFrame(list(itertools.product(df1.A,df2.B)),columns=['A','B'])

AB01a11b21  c
32a42b52  c

Solution 2:

The product() function will do what you want:

pd.DataFrame(list(itertools.product(df1.A,df2.B)),columns=['A','B'])

Definition of product():

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

Solution 3:

You can do so with pd.MultiIndex:

(pd.DataFrame(index=pd.MultiIndex.from_product([df1['A'], df2['B']], 
                                              names=['A','B']))
.reset_index())

Output:

AB01a11b21   c
32a42b52   c

Post a Comment for "How To Concatenate Combinations Of Rows From Two Different Dataframes?"