在下一个apply迭代python中使用apply fnc的输出

在下一个apply迭代python中使用apply fnc的输出,python,pandas,lambda,apply,Python,Pandas,Lambda,Apply,下面是一个1。启动df(称为“关闭”),以及2。应用代码行及其结果df: 一, 二, 我的预期输出是使用#2(100.706786)的输出和现有df“close”来计算序列中的下一个值,即2/03。该日期需要最后一个值(班次1)和之后的4个值(班次4或100) 如何仅使用矢量化来实现这一点?我想避免for循环,因为它非常慢。我有一个: closedf = pd.DataFrame() for num,date in enumerate(close.index[4:]): widget =

下面是一个1。启动df(称为“关闭”),以及2。应用代码行及其结果df:

一,

二,

我的预期输出是使用#2(100.706786)的输出和现有df“close”来计算序列中的下一个值,即2/03。该日期需要最后一个值(班次1)和之后的4个值(班次4或100)

如何仅使用矢量化来实现这一点?我想避免for循环,因为它非常慢。我有一个:

closedf = pd.DataFrame()
for num,date in enumerate(close.index[4:]):
    widget = close.apply(lambda x: x.shift(1) + (x.shift(4)).iloc[num+4]
    closedf[date] = close.iloc[num+4] = widget

考虑一系列
close

close = pd.Series(
    [100] * 3 + [100.706786] + [np.nan] * 10,
    pd.date_range('2006-01-27', periods=14, name='Date')
)

close

Date
2006-01-27    100.000000
2006-01-28    100.000000
2006-01-29    100.000000
2006-01-30    100.706786
2006-01-31           NaN
2006-02-01           NaN
2006-02-02           NaN
2006-02-03           NaN
2006-02-04           NaN
2006-02-05           NaN
2006-02-06           NaN
2006-02-07           NaN
2006-02-08           NaN
2006-02-09           NaN
Freq: D, dtype: float64

解决方案
这是斐波那契序列的导数。据我所知,我们不能“矢量化”那个。。。(w/e“矢量化”指)

但是我们可以创建一个执行该任务的生成器

def shib(x1, x2, x3, x4):
    while True:
        x1, x2, x3, x4 = x2, x3, x4, x1 + x4
        yield x4
然后用它来分配新的变量

from itertools import islice

close.iloc[4:] = list(islice(shib(*close[:4]), 0, len(close) - 4))

close

Date
2006-01-27     100.000000
2006-01-28     100.000000
2006-01-29     100.000000
2006-01-30     100.706786
2006-01-31     200.706786
2006-02-01     300.706786
2006-02-02     400.706786
2006-02-03     501.413572
2006-02-04     702.120358
2006-02-05    1002.827144
2006-02-06    1403.533930
2006-02-07    1904.947502
2006-02-08    2607.067860
2006-02-09    3609.895004
Freq: D, dtype: float64

考虑一系列
close

close = pd.Series(
    [100] * 3 + [100.706786] + [np.nan] * 10,
    pd.date_range('2006-01-27', periods=14, name='Date')
)

close

Date
2006-01-27    100.000000
2006-01-28    100.000000
2006-01-29    100.000000
2006-01-30    100.706786
2006-01-31           NaN
2006-02-01           NaN
2006-02-02           NaN
2006-02-03           NaN
2006-02-04           NaN
2006-02-05           NaN
2006-02-06           NaN
2006-02-07           NaN
2006-02-08           NaN
2006-02-09           NaN
Freq: D, dtype: float64

解决方案
这是斐波那契序列的导数。据我所知,我们不能“矢量化”那个。。。(w/e“矢量化”指)

但是我们可以创建一个执行该任务的生成器

def shib(x1, x2, x3, x4):
    while True:
        x1, x2, x3, x4 = x2, x3, x4, x1 + x4
        yield x4
然后用它来分配新的变量

from itertools import islice

close.iloc[4:] = list(islice(shib(*close[:4]), 0, len(close) - 4))

close

Date
2006-01-27     100.000000
2006-01-28     100.000000
2006-01-29     100.000000
2006-01-30     100.706786
2006-01-31     200.706786
2006-02-01     300.706786
2006-02-02     400.706786
2006-02-03     501.413572
2006-02-04     702.120358
2006-02-05    1002.827144
2006-02-06    1403.533930
2006-02-07    1904.947502
2006-02-08    2607.067860
2006-02-09    3609.895004
Freq: D, dtype: float64

事实上,我使用deque找到了一个非常方便(而且速度非常快)的解决方案:

from collections import deque

queue = deque([100]*(4))    
close = []
for num in range(0,len(close.index-4):
    nextval = queue[-1] + queue[0]
    close.append(nextval)
    queue.popleft()
    queue.append(nextval)
close = pd.DataFrame(close,index=close.index)

事实上,我使用deque找到了一个非常方便(而且速度非常快)的解决方案:

from collections import deque

queue = deque([100]*(4))    
close = []
for num in range(0,len(close.index-4):
    nextval = queue[-1] + queue[0]
    close.append(nextval)
    queue.popleft()
    queue.append(nextval)
close = pd.DataFrame(close,index=close.index)

恐怕不是100%确定一次手术就能做到。您基本上希望在下一次迭代中使用上一次迭代的结果,而不使用迭代!有什么方法可以加快我的功能吗?大部分人都不满意它所花的时间。。。你需要申请吗?不,这里没有规定,恐怕不是100%确定一次手术就可以。您基本上希望在下一次迭代中使用上一次迭代的结果,而不使用迭代!有什么方法可以加快我的功能吗?大部分人都不满意它所花的时间。。。你需要申请吗?不,这里没有规定,哈哈