pandas 查找值小于阈值的第一行

bvjveswy  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(134)

我有一个Pandas数据框,我尝试在left小于的值时找到第一个ID

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

所以我会得到一个列表或 Dataframe ,看起来像

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

我怎样才能做到这一点?

falq053o

falq053o1#

如果要匹配第一个大于目标值的值的ID,请使用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,
)

输出:

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 #
7uzetpgm

7uzetpgm2#

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]))

出局

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 #

相关问题