繁体   English   中英

Python在Pandas中反复查询最接近的值行

[英]Python querying closest value row repeatedly in Pandas

我在单个查询中看到了类似的问题: Pandas系列中的最近值

我需要做类似的事情,但要进行多个查询。

我有一列数据,就像一个时间输入,然后是另一列,它应该像一个函数一样工作。

例如:

df = pd.concat([pd.Series(np.random.rand(51), name = 'f'), pd.Series(np.sort(np.random.choice(range(150), 51, replace=False)), name = 't')], axis = 1)

给出前五行,例如:

df.head()
        f       t
0   0.459344    0
1   0.675319    3
2   0.481433    8
3   0.373959    12
4   0.554812    14

基本上,f列就像一个函数,在[0,3)区间中的值为0.459344,在[3,8)区间中的值为0.675319。

我正在使用一个自定义库进行集成,该库将在给定时间重复查询函数值,并按固定的步长(例如0.05)进行排序。 那时我必须为其提供函数的值。

例如:前5个查询将分别是0、0.05、0.1、0.15、0.2,所有查询均应返回0.459344

如何以最佳方式实现此目的而无需重复搜索?

编辑 :澄清输入和输出格式。

我必须创建一个应该以时间为输入的函数,并输出函数值。

输入时间可以是浮点数,这绝对是因为时间步长约为0.05。

例如:

让我们假设我已经创建了一个功能FunF,它可以实现我想要的功能。

它将由外部库这样调用

FunF(0) - # This should return 0.459344
FunF(0.05) - # This should return 0.459344
FunF(0.1) - # This should return 0.459344
FunF(0.15) - # This should return 0.459344
 ...
 ...
 ...
FunF(2.95) - # This should return 0.459344
FunF(3) - # This should return 0.675319
FunF(3.05) - # This should return 0.675319

等等。

我需要编写函数FunF。

我认为您将列与值x进行比较,并获得具有最后True值的行-例如,通过iatvalues

def FunF(x):
    return df.loc[(df['t'] <= x), 'f'].iat[-1]

替代方案:

def FunF(x):
    return df.loc[df['t'] <= x, 'f'].values[-1]


print (FunF(0))
0.459344
print (FunF(0.1))
0.459344
print (FunF(2.95))
0.459344

print (FunF(3))
0.675319
print (FunF(3.1))
0.675319
print (FunF(8))
0.554812
print (FunF(9))
0.554812

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM