Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Merging two dataframes based on dates that are overlapped using python

$
0
0

I have two dataframes, one that designates one characteristic and another that designates another characteristic, I wanted to join them, but in such a way that the result would be dependent of the intersection between dates.

df1:

Keyin_dateout_datechar1
0100001/01/202001/10/2020A
1100002/10/202010/12/2020B
2200001/01/201910/01/2019C
3200011/01/201901/10/2022D
4200002/10/202201/01/2023B
5200002/01/202331/12/2030L

df2

Keyst_dateend_datechar2
0100001/05/201901/09/2020G
1100002/09/202010/11/2020GG
2200020/01/201915/02/2019K
3200016/02/201910/01/2022GE
4200011/01/202231/12/2030GG

Desire result:

keystartendchar1char2
0100001/05/201931/12/2019nullG
1100001/01/202031/08/2020AG
2100001/09/202001/10/2020AGG
3100002/10/202010/11/2020AGG
4100011/11/202010/12/2020Bnan
5200001/01/201910/01/2019Cnan
6200011/01/201919/01/2019Dnan
7200020/01/201915/02/2019DK
8200016/02/201910/01/2022DGE
9200011/01/202201/10/2022DGG
10200002/10/202201/01/2023BGG
11200002/01/202331/12/2030LGG

I tried to use many if and else, but when I tried to aggregate the dataframe, didn't work.

I tried to use pd.merge but I have a sparse matrix


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>