我创建了一个价格数据框架,移动平均线,现在是“maX”列,突出显示2个移动平均线何时交叉;
OPEN HIGH LOW LAST ma5 ma8 ma21 maX
Date
11/23/2009 88.84 89.19 88.58 88.97 NaN NaN NaN 0.0
11/24/2009 88.97 89.07 88.36 88.50 NaN NaN NaN 0.0
11/25/2009 88.50 88.63 87.22 87.35 NaN NaN NaN 0.0
11/26/2009 87.35 87.48 86.30 86.59 NaN NaN NaN 0.0
11/27/2009 86.59 87.02 84.83 86.53 87.588 NaN NaN 0.0
11/30/2009 87.17 87.17 85.87 86.41 87.076 NaN NaN 0.0
12/1/2009 86.41 87.53 86.17 86.68 86.712 NaN NaN 0.0
12/2/2009 86.68 87.49 86.59 87.39 86.720 87.302 NaN 0.0
12/3/2009 87.39 88.48 87.32 88.26 87.054 87.214 NaN 0.0
12/4/2009 88.26 90.77 88.00 90.56 87.860 87.471 NaN 0.0
但是为什么我无法迭代他的新专栏?我的代码;
Buy = [0,]
maXLast = [0]
for i in maX[1:]:
if i == 1 and maXLast == 0:
Buy.append(1)
elif i == 1 and maX == -1:
Buy.append(0)
else:
Buy.append(0)
maXLast = i
print(Buy)
Entry = pd.DataFrame(Buy,index = dfmas.index).astype('float')
Entry.columns = ['Buy']
print(Entry)
但是为什么我的代码只返回[0,0]代表'买'而不是1850浮动列表。
那么为什么'回'会回来;
ValueError: Shape of passed values is (1, 2), indices imply (1, 1850) ???
非常感谢提前!
答案 0 :(得分:1)
不要在Pandas中使用for循环。相反,它是矢量化的方式,它将快一千倍:
import numpy as np
Buy = np.where(maX == 1, 1, 0)
无论如何都要这样 - 你可能需要在np.where()
中调整条件。