1

I have data frame of float numbers. I want to find closely matching number index.

df = 
index    value
0          10.53
1          20.23
2          12.34          
3          34.12
4          5.67
5          19.56

I want to find the closely matching value index for 9.5

matc_idx = df['value'].index.get_loc(9.5)

Output:

KeyError:

How to find the closely matching index. In this case, I am expecting

print(matc_idx) 
0

Previously accepted answers have used ix which is depreciated.

Mainland
  • 4,110
  • 3
  • 25
  • 56
  • `df.iloc[(df['value']-9.5).abs().argsort()[:1]].index` from this [thread](https://stackoverflow.com/questions/30112202/how-do-i-find-the-closest-values-in-a-pandas-series-to-an-input-number) – help-ukraine-now Aug 16 '19 at 19:01
  • 1
    @politicalscientist I tried your method and got following response `AttributeError: 'DataFrame' object has no attribute 'argsort'` – Mainland Aug 16 '19 at 19:11
  • sorry if I missed something. I replicated your df and run said code, it worked as expected, so not sure what's the problem :( – help-ukraine-now Aug 16 '19 at 19:23
  • `df['value'].sub(9.5).abs().sort_values().index[0]` or `df['value'].sub(9.5).abs().idxmin()` – Alexander Aug 16 '19 at 19:24

1 Answers1

4

Use idxmin to get the closest euclidean distance

VAL = 9.5
((df['value'] - VAL)**2).idxmin()

Outputs

 0
rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • 1
    your method worked. – Mainland Aug 16 '19 at 19:15
  • Not sure why euclidean distance is required. Surely absolute differences would suffice. In any event, `idxmin` would still work in either case. – Alexander Aug 16 '19 at 19:25
  • @Alexander for 1D, absolute difference is the euclidean ;p I just mentioned euclidean for R^N solutions - of course I omitted `sqrt` here to avoid redundancies – rafaelc Aug 16 '19 at 19:27