I have 2 dataframes and I want to check if the Start, End ranges in DF1 are within the Start, End ranges in DF2 and for the ones that are true I want to print the ID and the region. I want to compare each row of DF1 to each row of DF2.
These are my dataframes:
DF1 = pd.DataFrame ({'Start':[500, 850, 1000],
'End':[700, 950, 1200],
'Region':["A", "B", "C"]})
DF2 = pd.DataFrame ({'Start':[200, 800, 1100],
'End':[750, 950, 1250],
'ID':[1, 2, 3]})
DF1
| Start | End | Region |
|---|---|---|
| 500 | 700 | A |
| 850 | 950 | B |
| 1000 | 1200 | C |
| 1100 | 1500 | D |
DF2
| Start | End | ID |
|---|---|---|
| 200 | 750 | 1 |
| 800 | 950 | 2 |
| 1100 | 1250 | 3 |
I assume that I have to write a for loop to iterate through all the rows. However, I am a beginner and I am having a hard time setting it up correctly.
This is the code I have tried so far.
for Start, End in DF1:
if Start>=DF2["Start"] and End<=DF2["End"]:
print (DF1["Region"], DF2["ID"])
However, I am getting this error: ValueError: too many values to unpack (expected 2)
Any advice on how to solve this would be greatly appreciated.