繁体   English   中英

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

[英]Why can't I iterate through a new column in my data frame?

我创建了一个价格,移动平均线的数据框,现在创建了“ 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循环。 取而代之的是采用矢量化方法,它将快一千倍:

import numpy as np
Buy = np.where(maX == 1, 1, 0)

无论如何,您可能需要在np.where()调整条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM