Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append every row of pandas dataframe to every row of another dataframe

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

like image 506
user2963165 Avatar asked Dec 02 '22 21:12

user2963165


1 Answers

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
like image 189
Roman Pekar Avatar answered May 01 '23 11:05

Roman Pekar