【问题标题】:Why can't I iterate through a new column in my data frame?为什么我不能遍历数据框中的新列?
【发布时间】:2017-10-16 08:33:45
【问题描述】:

我创建了一个包含价格、移动平均线的数据框,现在创建了“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 浮动列表。

那么为什么'Enter'会返回;

ValueError: Shape of passed values is (1, 2), indices imply (1, 1850)    ???

非常感谢!

【问题讨论】:

    标签: algorithm python-3.x dataframe iteration


    【解决方案1】:

    不要在 Pandas 中使用 for 循环。取而代之的是矢量化方式,这将快一千倍:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 2015-12-02
      • 2014-12-11
      • 2021-11-26
      • 2012-09-27
      • 1970-01-01
      相关资源
      最近更新 更多