So a bit complicated example, at least for me. I have two dataframes. They have different interval lengths and I have a few criteria in order to merge them. I just don't know how to go about it. There will be multiple HoleIDs. Issue is overlapping intervals between both files and want the script to break it down if there is multiple values in each that it can break down to.
File1
HoleID,From,To,Value
DH-1,0,15,-99
DH-1,15,25,0.01
DH-1,25,100,0.022
File2
HoleID,From,To,Value
DH-1,0,5,0.08
DH-1,5,10,1
DH-1,10,25,0.04
DH-1,25,50,-99
DH-1,50,100,0.066
Want
HoleID,From,To,Value
DH-1,0,5,0.08
DH-1,5,10,1
DH-1,10,25,0.04
DH-1,25,50,0.022
DH-1,50,100,0.066
-99 = missing in this instance
Merge doesn't do it.
You can merge
the two frames on HoleID
and From
and conditionally update Value
if equal to -99
with mask
:
df3 = df2.merge(df1.drop('To', axis=1), on=['HoleID', 'From'], how='left', suffixes=['', '_y'])
df3['Value'] = df3['Value'].mask(df3['Value'] == -99, df3['Value_y'])
df3 = df3.drop('Value_y', axis=1)
df3
Out[1]:
HoleID From To Value
0 DH-1 0 5 0.080
1 DH-1 5 10 1.000
2 DH-1 10 25 0.040
3 DH-1 25 50 0.022
4 DH-1 50 100 0.066
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.