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

Python在Pandas中重复查询最近的值行,python,pandas,Python,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()

我在一个查询中看到了一个类似的问题:

我需要做一些类似的事情,但是对于多个查询

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

例如:

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

例如:

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)
让我们假设我已经创建了一个函数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进行比较,并将行与最后一个真值进行比较-例如,按或:

备选方案:

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

a列在您的答案中代表什么?@VikashB-这是输入数据,但正如我所说,在我的示例中,时间步长可能非常小,这意味着输入数据的行数将比我的行数多得多。@VikashB-我似乎不明白。预期的输出是什么?我已经对函数t的行为进行了编辑我必须写一顶帽子。