Reputation: 752
I am using the following to do a left join in Pandas:
merged_left = pd.merge(left=xrf_df,
right=statistics_and_notes_df,
how='left',
left_on=depth_column_name,
right_on='Core Depth')
however the depth_column_name and 'Core Depth' columns are floating point numbers. Is there a good way to do this left join such that the comparison is approximately equal such as np.isclose()?
Upvotes: 4
Views: 1123
Reputation: 210922
Assuming we have the following DFs:
In [111]: a
Out[111]:
a b c
0 3.03 c 3
1 1.01 a 1
2 2.02 b 2
In [112]: b
Out[112]:
a x
0 1.02 Z
1 5.00 Y
2 3.04 X
Let's set joining float64 column as index (sorted):
In [113]: a = a.sort_values('a').set_index('a')
In [114]: b = b.assign(idx=b['a']).set_index('idx').sort_index()
In [115]: a
Out[115]:
b c
a
1.01 a 1
2.02 b 2
3.03 c 3
In [116]: b
Out[116]:
a x
idx
1.02 1.02 Z
3.04 3.04 X
5.00 5.00 Y
now we can use DataFrame.reindex(..., method='nearest'):
In [118]: a.join(b.reindex(a.index, method='nearest'), how='left')
Out[118]:
b c a x
a
1.01 a 1 1.02 Z
2.02 b 2 1.02 Z
3.03 c 3 3.04 X
In [119]: a.join(b.reindex(a.index, method='nearest'), how='left').rename(columns={'a':'a_right'})
Out[119]:
b c a_right x
a
1.01 a 1 1.02 Z
2.02 b 2 1.02 Z
3.03 c 3 3.04 X
In [120]: a.join(b.reindex(a.index, method='nearest'), how='left').rename(columns={'a':'a_right'}).reset_index()
Out[120]:
a b c a_right x
0 1.01 a 1 1.02 Z
1 2.02 b 2 1.02 Z
2 3.03 c 3 3.04 X
PS you may want to use df.reindex(..., tolerance=<value>)
parameter in order to set the tolerance: abs(index[indexer] - target) <= tolerance
Upvotes: 2