Skip to content
Advertisement

Join pandas dataframes based on different conditions

I´m using Pandas in Python and I’d like to join 2 dataframes. My first dataframe is:

id var date
1 ABCD 2019-01-01
1 ABCD 2017-06-01
1 ABCD 2016-06-01
2 ABCD 2016-01-01

The dataframe I want to join is:

var date
ABCD 2019-01-01
ABCD 2017-06-01
FJKL 2016-06-01
ABCD 2016-01-01

I want to join the firt dataframe by var and date but the condition is that the date of the second dataframe has to be smaller than the first. If I were in sql I would do:

Any ideas to implement with pandas?

Advertisement

Answer

There is merge and query:

Output:

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