在较大的列中找到大于或等于较短的搜索列中的每个值的第一个值

时间:2019-06-20 20:34:00

标签: python pandas vectorization

我一直在尝试找到一种矢量化方法,以使大列(> 500k行)中的第一个值的索引大于或等于短列(〜9k行)中的每个值。

当前,im遍历较短列中的每个值,并将其与整个较大列进行比较。循环数=较短列的长度。

   $email = (new Email())
        ->from('test@mail.com')
        ->to('foo@bar')
        ->subject('Send email test')
        ->text('email text');

    $this->mailer->send($email); // is there any way to return response result instead void

1 个答案:

答案 0 :(得分:0)

您应该考虑以下内容:

def myfunc(x):
    try:    
        return dfLong[dfLong.Long>=x].index[0]
    except:
        return None

dfShort['Location'] = dfShort.Short.apply(lambda x: myfunc(x))
dfShort['Value'] = dfShort.Location.apply(lambda x: dfLong.iloc[x, 0] if x!= None else None)
print(dfShort.head())

输出

+----+---------+-----------+--------+
|    | Short   | Location  | Value  |
+----+---------+-----------+--------+
| 0  | 0.0636  |       10  | 0.0674 |
| 1  | 0.0876  |       27  | 0.0938 |
| 2  | 0.0799  |       16  | 0.0831 |
| 3  | 0.0977  |       95  | 0.0997 |
| 4  | 0.0602  |       10  | 0.0674 |
+----+---------+-----------+--------+