I tried to merge two dataframes with respect to a column which are not having exact same values.
Below given is DF1
+--------+-----+------+
| NUM_ID | TIME|SG1_V |
+--------+-----+------+
|XXXXX01 |1001 |79.0 |
|XXXXX01 |1005 |88.0 |
|XXXXX01 |1010 |99.0 |
|XXXXX01 |1015 |null |
|XXXXX01 |1020 |100.0 |
|XXXXX02 |1001 |81.0 |
|XXXXX02 |1010 |91.0 |
|XXXXX02 |1050 |93.0 |
|XXXXX02 |1060 |93.0 |
|XXXXX02 |1070 |93.0 |
+--------+-----+------+
Below is DF2
+---------+-----+------+
| NUM_ID | TIME|SG2_V |
+---------+-----+------+
|XXXXX01 |1001 | 99.0|
|XXXXX01 |1003 | 22.0|
|XXXXX01 |1007 | 85.0|
|XXXXX01 |1011 | 1.0 |
|XXXXX02 |1001 | 22.0|
|XXXXX02 |1009 | 85.0|
|XXXXX02 |1048 | 1.0 |
|XXXXX02 |1052 | 99.0|
+---------+-----+------+
I have to join these two DF on columns NUM_ID, which should be exactly same and on column TIME which may/may not be exact value.
The TIME in DF2 may/may not contain exact value as in the DF1. If the value is not exact then, I have to join with the highest nearest value available (ie - column value in DF2 should be =< Exact Value in DF1).
It will be more clear after looking at the expected output shown below.
+--------+-----+------+-----+------+
| NUM_ID | TIME|SG1_V | TIME|SG2_V |
+--------+-----+------+-----+------+
|XXXXX01 |1001 |79.0 |1001 | 99.0|
|XXXXX01 |1005 |88.0 |1003 | 22.0|
|XXXXX01 |1010 |99.0 |1007 | 85.0|
|XXXXX01 |1015 |null |1011 | 1.0 |
|XXXXX01 |1020 |100.0 |1011 | 1.0 |
|XXXXX02 |1001 |81.0 |1001 | 22.0|
|XXXXX02 |1010 |91.0 |1009 | 85.0|
|XXXXX02 |1050 |93.0 |1048 | 1.0 |
|XXXXX02 |1060 |93.0 |1052 | 99.0|
|XXXXX02 |1070 |93.0 |1052 | 99.0|
+--------+-----+------+-----+------+
For NUM_ID XXXXX01, TIME(1005) in DF1 is not available in DF2, so it took nearest value (1003) which is smaller than 1005.
How to join in such a way that if exact value is not available, then join with nearest value.
Appreciate any leads. Thanks in Advance.