I am looking for a way to merge two dataframe based on two columns containing ids that could be considered as id1, id2 pair or id2, id1 pair.
I found a similar question to what I am looking for here but it doesn't help me with getting the output I need.
I am working with two dataframes df and temp and each of them have two columns with ids in them:
df:
time level_0 level_1
0 60.0 id1 id2
1 420.0 id2 id3
2 120.0 id3 id4
3 336.0 id4 id5
temp:
number level_0 level_1 length width
0 0 id1 id2 25 2
1 1 id4 id5 56 3
2 2 id4 id3 12 7
3 3 id2 id3 750 2
I would like to merge them to get a final dataframe with level_0 and level_1 with time, number, length and width.
As you can see in temp two of the ids are reversed in row 2 and that doesn't allow me to merge both dataframe successfully and I get None values in the selected columns.
Also the accepted answer of the post quoted above doesn't seem to work as I get a TypeError: '<' not supported between instances of 'tuple' and 'str' error.
Is there a way of doing a merge based on two columns knowing that some of their values could be in reverse order?
Edit:
After reading @SoheilPourbafrani comment, I tried using sets to compare level_0 and level_1 in both dataframes. However, I came across the fact that rows in temp are not in the same order as in df and don't allow me to compare rows between one another.
Here's the desired output for better understanding:
time level_0 level_1 number length width
0 60.0 id1 id2 0 25 2
1 420.0 id2 id3 3 750 2
2 120.0 id3 id4 2 12 7
3 336.0 id4 id5 1 56 3
