Find first row where value is less than threshold

Question:

I have a pandas data frame where I try to find the first ID for when the left is less than the values of

list = [0,50,100,150,200,250,500,1000]
             ID  ST  ...          csum           left
0             0  AK  ...  4.293174e+05  760964.996900
1             1  AK  ...  4.722491e+06  760535.679500
2             2  AK  ...  8.586347e+06  760149.293900
3             3  AK  ...  2.683233e+07  758324.695200
4             4  AK  ...  2.962290e+07  758045.638900
..          ... ...  ...           ...            ...
111         111  AK  ...  7.609006e+09     107.329336
112         112  AK  ...  7.609221e+09      85.863469
113         113  AK  ...  7.609435e+09      64.397602
114         114  AK  ...  7.609650e+09      42.931735
115         115  AK  ...  7.610079e+09       0.000000

So I would end up with a list or dataframe looking like

threshold      ID
        0      115
       50      114
      100      112
      150      100
      200      100
      250       99
      500       78
     1000       77

How can I achieve this?

Asked By: Thomas

||

Answers:

If you want to match the ID of the first value greater than the target, use a merge_asof:

lst = [0,50,100,150,200,250,500,1000]

pd.merge_asof(pd.Series(lst, name='threshold', dtype=df['left'].dtype),
              df.sort_values(by='left').rename(columns={'left': 'threshold'})[['threshold', 'ID']],
              # uncomment for strictly superior
              #allow_exact_matches=False,
)

Output:

   threshold   ID
0        0.0  115
1       50.0  114
2      100.0  112
3      150.0  111 # due to truncated input
4      200.0  111 #
5      250.0  111 #
6      500.0  111 #
7     1000.0  111 #
Answered By: mozway
list = [0,50,100,150,200,250,500,1000]
df11=pd.DataFrame(dict(threshold=list))
df11.assign(id=df11.threshold.map(lambda x:df1.query("left<=@x").iloc[0,0]))
  

out

   threshold   ID
0        0.0  115
1       50.0  114
2      100.0  112
3      150.0  111 # due to truncated input
4      200.0  111 #
5      250.0  111 #
6      500.0  111 #
7     1000.0  111 #
Answered By: G.G
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.