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
假设我们有以下DFs:
让我们将连接float64列设置为索引(已排序):
^{pr2}$现在我们可以使用DataFrame.reindex(..., method='nearest'):
PS您可能需要使用
df.reindex(..., tolerance=<value>)
参数来设置公差:abs(index[indexer] - target) <= tolerance
相关问题 更多 >
编程相关推荐