0

I have two dataframes:

df1 = pd.DataFrame({'id1':[3,2,1,4,5],'id2':[8,7,6,9,10]})
df2 = pd.DataFrame({'id3':[6,2,8,5,4],'Name':['a','b','c','e','d']})

df1

id1   id2
3     8
2     7
1     6
4     9
5     10

df2


id3   Name
6     a
2     b
8     c
5     e
4     d

Description

  • df1 consists of 2 columns (id1 and id2) which represents unique values for both the columns.

  • df2 consists of 2 columns (id3 and name) which also represents unique values.

  • id3 column of df2 is made up of either of values from df1 rows and its order is also random. Example - Lets take the first value of id3 column which is 6. It is made by picking a random value the third row of df1. Similarly 2 is made from picking random value from second row of df1. and so on..

Now I want to merge these 2 dataframes by id3 of df2 and either of id1 or id2 of df1 based on availability on id3 column. So, the final dataframe would look like this:

id1 id2 id3 Name
3   8   8   c
2   7   2   b
1   6   6   a
4   9   4   d
5   10  5   e

Can anyone help me on this?

3
  • 1
    Why not add an id3 column to df1 using that logic instead of doing it in a separate step? E.g., df1["id3"] = df1.apply(lambda r: random.choice(r[["id1", "id2"]]), axis=1) and then assign df1["name"] = list("abcde")?
    – ddejohn
    Sep 28, 2021 at 19:21
  • what would be the expected output for df1 = pd.DataFrame({'id1':[2,6,3,4,5],'id2':[6,7,8,9,10]}) with df2 unchanged?
    – mozway
    Sep 28, 2021 at 19:27
  • @ddejohn df1 and df2 dataframes are just a sample from a huge actual dataframes which is already available Sep 28, 2021 at 19:49

1 Answer 1

2

You can merge 2 times: first on id1/id3 and next on id2/id3 then join the 2 dataframes:

Update

I want to restore the order of values of df1 in out dataframe as well.

out = pd.concat([df1.reset_index().merge(df2, left_on='id1', right_on='id3'),
                 df1.reset_index().merge(df2, left_on='id2', right_on='id3')]) \
        .sort_values('index').drop(columns='index').reset_index(drop=True)

Output:

>>> out
   id1  id2  id3 Name
0    1    6    6    a
1    2    7    2    b
2    3    8    8    c
3    4    9    4    d
4    5   10    5    e
4
  • Instead of sorting by Name column, can you restore the order of values from df1 which automatically order Name as well? Sep 28, 2021 at 19:51
  • Like .sort_values(['id1', 'id2'])?
    – Corralien
    Sep 28, 2021 at 19:53
  • the dataframes values can be any instead of numbers or alphabets. So, it would not be ideal to sort the dataframe based on column values. I want to restore the order of values of df1 in out dataframe as well. I will update my question which might help you understand better Sep 28, 2021 at 19:59
  • 1
    I updated my answer. I reset the index from df1 to keep order on merge then sort values from the column index
    – Corralien
    Sep 28, 2021 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.