Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 为什么我不能遍历数据框中的新列?_Algorithm_Python 3.x_Dataframe_Iteration - Fatal编程技术网

Algorithm 为什么我不能遍历数据框中的新列?

Algorithm 为什么我不能遍历数据框中的新列?,algorithm,python-3.x,dataframe,iteration,Algorithm,Python 3.x,Dataframe,Iteration,我已经创建了一个价格、移动平均线的数据框架,现在是“maX”列,当两个移动平均线交叉时突出显示 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/2

我已经创建了一个价格、移动平均线的数据框架,现在是“maX”列,当两个移动平均线交叉时突出显示

             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)    ???

非常感谢

不要将for循环用于Pandas。相反,采用矢量化的方式,速度大约快一千倍:

import numpy as np
Buy = np.where(maX == 1, 1, 0)
不管怎样,你可能需要调整一下条件