0
votes

I have a DataFrame like this:

    D1   W1
0.1 23.7 22.4
0.2 25.8 26.88
0.3 29.8 30.64
.
.
.
1.0 24.85 38.31

I will give it a value. How to find it in which range of this table?

For example, if I give a value of 27.33, how to find it between 0.2 and 0.3 and then tell me that the upper limit is 29.8 and the lower limit is 25.8?

1
indexing by float is not the best idea...Quang Hoang
df[(0.2 < df.index) & (df.index <= 0.3)].describe() will give you information including the min/max.anon01
@QuangHoang whats the downside to using floats as an index?anon01
@anon01 due to floating point precision, one essentially cannot guarantee access by index.Quang Hoang
OK, I will change the index precision to int thx!blueman010112

1 Answers

1
votes

Try with searchsorted from numpy

idx = np.searchsorted(df.D1,27.33)
df.iloc[idx-1:idx+1,:]
       D1     W1
0.2  25.8  26.88
0.3  29.8  30.64

Finding the lower

df.iloc[idx-1,:]['D1']
25.8