6

for example, df1 is a 3*2 dataframe, and df2 is a 10*3 dataframe, what I want is to generate a new dataframe of 30*5, where each row in df1 is appended with the 3 columns of df2 for all 10 rows in df2.

I know I can use iteration to append columns of df2 to each row of df1, but I am wondering whether there are some more efficient way to do this in pandas, like its concat functions.

could anyone help?

regards, nan

1 Answer 1

19

If I understand you, you need cartesian product. You can emulate this with merge in pandas:

>>> df1 = pd.DataFrame({'A':list('abc'), 'B':range(3)})
>>> df2 = pd.DataFrame({'C':list('defg'), 'D':range(3,7)})
>>> df1['key'] = 1
>>> df2['key'] = 1
>>> df = pd.merge(df1, df2, on='key')
>>> del df['key']
>>> df
    A  B  C  D
0   a  0  d  3
1   a  0  e  4
2   a  0  f  5
3   a  0  g  6
4   b  1  d  3
5   b  1  e  4
6   b  1  f  5
7   b  1  g  6
8   c  2  d  3
9   c  2  e  4
10  c  2  f  5
11  c  2  g  6
Sign up to request clarification or add additional context in comments.

1 Comment

@DanAllan I've opened an issue here github.com/pydata/pandas/issues/5401 I think it's easy to add parameter value how='cross' to merge and join. Don't know if devs think it's appropriate, if they're, I think I could create PR :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.