I have two datasets that have an ID that overlaps. To make this shorter, I'm only going to post the ID that has an overlap. For the From/To interval that overlaps, I want to choose the second dataset, df2, except with Python I don't know how to do it. I know its probably easier/easiest with SQL but I want to know if it is possible with Python. There are extra variables in df2 that I want to come along for the ride but for the variables that are the same, I want to choose df2 instead of df1 for the From/To overlap between the two.
df1
| ID | From | To | Q | RM | RQ |
|---|---|---|---|---|---|
| MRC-17 | 447 | 472 | 0.63 | 42 | 10 |
| MRC-17 | 472 | 502 | 2.5 | 42 | 20 |
| MRC-17 | 502 | 503.8 | 2.5 | 37 | 10 |
| MRC-17 | 503.8 | 509.7 | 0.42 | 29 | 10 |
| MRC-17 | 509.7 | 527 | 0.38 | 32 | 10 |
| MRC-17 | 527 | 545 | 0.38 | 32 | 10 |
| MRC-17 | 545 | 551 | 3.33 | 47 | 26.67 |
| MRC-17 | 551 | 576 | 0.38 | 32 | 10 |
| MRC-17 | 576 | 579.5 | 6.07 | 47 | 48.57 |
| MRC-17 | 579.5 | 597 | 0.38 | 32 | 10 |
| MRC-17 | 597 | 616 | 0.38 | 32 | 10 |
| MRC-17 | 616 | 626 | 4.75 | 47 | 38 |
| MRC-17 | 626 | 647 | 0.38 | 32 | 10 |
| MRC-17 | 647 | 662 | 0.83 | 34 | 10 |
| MRC-17 | 662 | 677 | 0.38 | 37 | 10 |
df2
| ID | From | To | H | DP | DR | IV | No | RQ | RM | Q |
|---|---|---|---|---|---|---|---|---|---|---|
| MRC-17 | 499 | 504 | 1 | U | S | D | 7 | 50 | 32 | 2.08 |
| MRC-17 | 504 | 510 | 2 | P | R | D | 7 | 25 | 32 | 0.78 |
| MRC-17 | 510 | 545 | 0 | P | K | F | 9 | 5 | 18 | 0.02 |
| MRC-17 | 545 | 565 | 0 | P | K | F | 8 | 60 | 28 | 0.33 |
| MRC-17 | 565 | 575 | 0 | P | K | F | 9 | 5 | 18 | 0.02 |
| MRC-17 | 575 | 581 | 1 | P | K | F | 7 | 70 | 34 | 0.49 |
| MRC-17 | 581 | 600 | 0 | P | K | F | 8 | 20 | 23 | 0.11 |
| MRC-17 | 600 | 612 | 0 | P | K | F | 9 | 5 | 18 | 0.02 |
| MRC-17 | 612 | 634 | 1 | P | S | C | 7 | 70 | 38 | 2.92 |
| MRC-17 | 634 | 647 | 0 | P | S | F | 9 | 5 | 22 | 0.04 |
| MRC-17 | 647 | 662 | 2 | P | S | B | 7 | 55 | 39 | 4.58 |
| MRC-17 | 662 | 677 | 0 | P | S | F | 9 | 15 | 22 | 0.13 |
Resulting in Final (-99 means missing for numeric, X for char):
| ID | From | To | H | DP | DR | IV | No | RQ | RM | Q |
|---|---|---|---|---|---|---|---|---|---|---|
| MRC-17 | 447 | 472 | -99 | X | X | X | -99 | 10 | 42 | 0.63 |
| MRC-17 | 472 | 499 | -99 | X | X | X | -99 | 20 | 42 | 2.50 |
| MRC-17 | 499 | 504 | 1 | U | S | D | 7 | 50 | 32 | 2.08 |
| MRC-17 | 504 | 510 | 2 | P | R | D | 7 | 25 | 32 | 0.78 |
| MRC-17 | 510 | 545 | 0 | P | K | F | 9 | 5 | 18 | 0.02 |
| MRC-17 | 545 | 565 | 0 | P | K | F | 8 | 60 | 28 | 0.33 |
| MRC-17 | 565 | 575 | 0 | P | K | F | 9 | 5 | 18 | 0.02 |
| MRC-17 | 575 | 581 | 1 | P | K | F | 7 | 70 | 34 | 0.49 |
| MRC-17 | 581 | 600 | 0 | P | K | F | 8 | 20 | 23 | 0.11 |
| MRC-17 | 600 | 612 | 0 | P | K | F | 9 | 5 | 18 | 0.02 |
| MRC-17 | 612 | 634 | 1 | P | S | C | 7 | 70 | 38 | 2.92 |
| MRC-17 | 634 | 647 | 0 | P | S | F | 9 | 5 | 22 | 0.04 |
| MRC-17 | 647 | 662 | 2 | P | S | B | 7 | 55 | 39 | 4.58 |
| MRC-17 | 662 | 677 | 0 | P | S | F | 9 | 15 | 22 | 0.13 |
Thanks in advance for all the help!
So far all I've done is load the data:
# Load libraries
import pandas as pd
import numpy as np
from scipy import stats
df1 = pd.read_csv('LOGGED_DATA.csv')
df2 = pd.read_csv('PHOTOLOGGED_DATA.csv')
But I'm having a hard time trying to figure out how to go about this. I looked at inner, outer, etc joins. But having the interval overlap, is throwing it off!