3

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.

Webcy
  • 181
  • 8
  • Do you want to ignore the values in `col1`, `col2` and `col3` from df2? – razdi May 10 '19 at 04:25
  • Possible duplicate of [Append column to pandas dataframe](https://stackoverflow.com/questions/20602947/append-column-to-pandas-dataframe) – razdi May 10 '19 at 04:28

3 Answers3

3

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
gmds
  • 19,325
  • 4
  • 32
  • 58
1

You can use:

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

  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.0       34       87     66
2     b     z    NaN       64        9      7
3     c     z    7.0       23       51     53
heena bawa
  • 818
  • 6
  • 5
0

Just use the merge function available

import pandas as pd

df=df1.merge(df2,left_index=True)
Ajay Shah
  • 414
  • 5
  • 10