I have two dataframes as shown below:
result1
time browncarbon blackcarbon
180.7452 0.506824055392119 0.4693240205237933
180.748 0.5040641475588111 0.4671092323195378
180.7508 0.49911820575405846 0.46344714546409305
180.7535 0.4957944583911674 0.46030629341216533
180.7563 0.4888745617073804 0.45557451231658985
180.7591 0.4864626914800723 0.45633142113414893
180.7619 0.48328511735148877 0.4548510376145042
180.7646 0.484728828747634 0.4572818652186026
180.7674 0.4840750981022636 0.45772491443336777
180.7702 0.4843291425046101 0.4588332952196751
422 rows x 3 columns
result2
start end toc
180.7452 180.7466 192.0
180.7438 180.7452 194.0
180.7424 180.7438 199.0
180.741 180.7424 208.0
180.7396 180.741 229.0
180.7383 180.7396 245.0
180.7369 180.7383 252.0
180.7355 180.7369 245.0
180.7341 180.7355 238.0
180.7327 180.7341 245.0
1364 rows x 3 columns
The multiple start and end rows that get encapsulated into one of the time rows should also correspond to one toc row, which should be the average of the multiple toc rows. How do I do that? There is a related answer on Stack Overflow. The link is: Merging two pandas dataframes with complex conditions
result3
result1['rank'] = np.arange(length1)
result3=pd.merge_asof(result1.sort_values('time'),result2,left_on='time',right_on='start')
result3.sort_values('rank').drop(['rank','start','end'], axis=1)
time browncarbon blackcarbon toc
180.7452 0.506824055392119 0.4693240205237933
180.748 0.5040641475588111 0.4671092323195378
180.7508 0.49911820575405846 0.46344714546409305
180.7535 0.4957944583911674 0.46030629341216533
180.7563 0.4888745617073804 0.45557451231658985
180.7591 0.4864626914800723 0.45633142113414893
180.7619 0.48328511735148877 0.4548510376145042
180.7646 0.484728828747634 0.4572818652186026
180.7674 0.4840750981022636 0.45772491443336777
180.7702 0.4843291425046101 0.4588332952196751
422 rows X 4 columns