I can't seem to find this example anywhere. Having trouble determining if I need to use indexes, boolean mask, or if a direct merge is possible. I've tried variations of .isin and .between each to no success.
Scenario:
Two dataFrames with no common index:
df1 = pd.DataFrame({'printId': ['x','y', 'z', 'a'],'locCode': [0.9, 1.5, 4.0, 7.8]}) df2 = pd.DataFrame({'assetId': ['1','1a', '2', '2a', '3', '4'], 'locStart': [0.9, 0.9, 1, 1, 4, 8], 'locEnd': [0.9, 0.9, 3, 3, 5, 13]})
df1:
df2:
Need this:
df3 = pd.DataFrame({'printId': ['x','x', 'y', 'y', 'z', 'a', 'NaN'], 'locCode': ['0.9', '0.9', '1.5', '1.5', '4.0', '7.8', 'NaN'], 'assetID': ['1', '1a', '2', '2a', '3', 'NaN', '4'], 'locStart': ['0.9', '0.9', '1.0', '1.0', '4.0', 'NaN', '8.0'], 'locEnd':['0.9', '0.9', '3.0', '3.0', '4.0', 'NaN', '13.0']})
df3
How do professionals attack this problem?
EDITED: Original answer did not work after closer examination.
- Where
df2has duplicatedlocStart/Endrecords, but uniqueassetID(row 0, 1 and row 2 and 3),df1will not merge.


