Reputation: 475
in pandas I want to introduce a new column 'Neighbor' in my dataframe which saves (e.g.) the index or the value of another row, for which a certain column B is most close to the value of the current row. E.g.
A Neighbor
0 5 1
1 4 0
2 1 3
3 2 2
4 10 0
Do you know a pandas function doing that? As my dataset is very big apply unfortunately is too slow.
Thanks!
Upvotes: 0
Views: 496
Reputation: 438
Something like this might do the trick:
df['myindex'] = df.index
# result = df.sort(['A'])
result = df.sort_values(by='A')
result['NeighborIndex'] = result['myindex'].shift(-1)
Upvotes: 1
Reputation: 323376
IDK, whether you check my update answer for your previous question or not, this is the way I am using
a=df.A.values[:,None]-df.A.values
np.fill_diagonal(a, 9999999)
np.argmin(abs(a),0)
Out[1160]: array([1, 0, 3, 2, 0], dtype=int64)
Upvotes: 1