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.

2
  • Do you want to ignore the values in col1, col2 and col3 from df2? Commented May 10, 2019 at 4:25
  • Possible duplicate of Append column to pandas dataframe Commented May 10, 2019 at 4:28

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

4 Comments

didn't notice the weird request (thought he just wanted it sorted based on df1.). good catch.
oh... I'm so sorry. df2 's values in col_4 and col_5 might change the order in to df1.
I have modified the result. This is my first time to ask the question, sorry
@zero Yes, it is rather strange. I originally also gave a merge-based answer.
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

Comments

0

Just use the merge function available

import pandas as pd

df=df1.merge(df2,left_index=True)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.