Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two dataframes with same values in several columns

I would like to combine two datframe with same index number but different columns number like:

>>> df1
   col_1 col_2 col_3 col_4
0    a     x    NaN    54
1    a     y     5     34
2    b     z    NaN    64
3    c     z     7     23


>>> df2
   col_1 col_2 col_3 col_4 col_5
0    a     x    NaN    14    14
1    b     z    NaN     9     7
2    c     z     7     51    53
3    a     y     5     87    66

The df2 will combine in df1 based on the values of col_1, col_2 andcol_3.

But the order of the rows will not be the same.

I want to combine them based on the order of df1

And the answer will be like this:

   col_1 col_2 col_3 col_4 col_4 col_5
0    a     x    NaN    54    14    14
1    a     y     5     34    87    66
2    b     z    NaN    64     9     7
3    c     z     7     23    51    53

I don't care about the column name, so you guys can modify them if you need.

like image 990
Webcy Avatar asked Feb 27 '26 21:02

Webcy


1 Answers

If I understand correctly, you want to join on the indices without sorting:

result = df1.join(df2.drop(columns=['col_1', 'col_2', 'col_3']), lsuffix='x', rsuffix='y', sort=False)
print(result)

Output:

  col_1 col_2 col_3 col_4x col_4y col_5
0     a     x   NaN     54     14    14
1     a     y     5     34      9     7
2     b     z   NaN     64     51    53
3     c     z     7     23     87    66

Otherwise, a simple merge on the first three columns, again without sorting, will do:

result = df1.merge(df2, on=['col_1', 'col_2', 'col_3'], sort=False)
print(result)

Output:

  col_1 col_2 col_3 col_4_x col_4_y col_5
0     a     x   NaN      54      14    14
1     a     y     5      34      87    66
2     b     z   NaN      64       9     7
3     c     z     7      23      51    53
like image 88
gmds Avatar answered Mar 01 '26 11:03

gmds