- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面是 1. 开始 df(称为“关闭”),和 2. 应用代码行及其生成的 df:
1.
Date
2006-01-27 100.0
2006-01-30 100.0
2006-01-31 100.0
2006-02-01 100.0
2006-02-02 NaN
2006-02-03 NaN
2.
close.apply(lambda x: x.shift(1) + (x.shift(4))
Date
2006-01-27 NaN
2006-01-30 NaN
2006-01-31 NaN
2006-02-01 NaN
2006-02-02 100.706786
2006-02-03 NaN
我的预期输出是使用 #2 (100.706786) 的输出和现有的 df“关闭”来计算序列中的下一个值,即 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
关于python - 在下一次应用迭代 python 中使用应用 fnc 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47216419/
Java 中有没有办法做这样的事情: void fnc(void Reference_to_other_func()); 我正在尝试的基本上是我有很多地方需要向用户显示相同的文本,唯一的区别是在该文本
有那些: template struct Is_Node { enum {value = false}; }; template class Node;//frwd decl. templat
我有一个 for 循环,我想将其转换为 parallel_for fnc 调用。我的代码满足此转换成功的所有条件(在 Parallel_Programming_with_Microsoft_Visua
我有一个自定义数据类型数组和 vector ,如下所示。在 Foo 函数中,我开始用数据填充数组和 vector 。无论如何,用数据填充数组没有问题。但是我无法使用 vector 访问任何内容。我找不
这个问题在这里已经有了答案: What is the actual HEX / binary value of the GS1 FNC1 character? (2 个答案) 关闭 4 年前。 如何
下面是 1. 开始 df(称为“关闭”),和 2. 应用代码行及其生成的 df: 1. Date 2006-01-27 100.0 2006-01-30 100.0 2006-01-31
我有一个这种形式的结构: template struct Base { X get_data() { return X(); } }; template struct Derive
我正在尝试熟悉委托(delegate)和 http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx ,我正在阅读: “与 C 或 C
我是一名优秀的程序员,十分优秀!