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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With