Skip to content
Advertisement

Is there a way to merge on Interval Index and another Column Value in pandas?

So I currently have 2 dataframes. These have different columns and what I have been trying to figure out is how to merge on an interval index as well as a unique ID value. Below are 2 different examples of the dataframes I have:

JavaScript

Creating the dataframe:

JavaScript

Creating the dataframe:

JavaScript

What I want to do is to be able to merge on the UniqueID as well as the interval of the start date and end date inclusively. The resultant dataframe would look like the one below:

JavaScript

The first method I have thought of using is to use an IntervalIndex on df1 and then merge based off that but then I have the issue of not being able to merge on the UniqueID and vice versa with UniqueID as a merge column. I kept with a left join when I merged df2 with df1 in order to preserve the original dataframe while merging any records of df1 with potential “matches” on df2.

I thought of potentially using a MultiIndex with an IntervalIndex as one of the levels and then the UniqueID as another but wasn’t sure how to go about this? Any ideas would be greatly appreciated!

The code below, should allow you to get the dataframes into a pandas df. Just make sure to copy and reassign.

JavaScript

Advertisement

Answer

Merge your dataframe on your UniqueID column then check if Trip_Date is between Start_Date and End_date. Finally, set to nan all rows where the condition is not met:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement