【问题标题】:For loop, how to reduece running time, pandas dataframeFor循环,如何减少运行时间,熊猫数据框
【发布时间】:2016-06-19 11:09:58
【问题描述】:
  Date      Open    Close   Buying  Selling Cut_Off_Price
2009.11.11  4.805   4.43    0        0  0
2009.11.12  4.51    4.505   0        0  0
2009.11.13  4.545   4.765   1        0  3.812
2009.11.16  4.78    4.76    0        0  0
2009.11.17  4.755   4.605   0        0  0
2009.11.18  4.56    4.495   0        0  0
2009.11.19  4.495   4.535   1        0  3.628
2009.11.20  4.535   4.63    0        0  0
2009.11.23  4.7     4.67    0        0  0 
2009.11.24  4.74    4.91    0       0   0
2009.11.25  4.97    4.87    0        0  0
2009.11.26  4.93    4.97    0        0  0
2009.11.27  5       4.94    0        0  0
2009.11.30  4.865   4.86    0        0  0
2009.12.1   4.83    3.855   0        0  0
2009.12.2   4.89    4.89    0        0  0
2009.12.3   4.85    4.71    0        0  0
2009.12.4   4.78    4.76    0        0  0
2009.12.7   3.225   3.565   0        0  0
2009.12.8   3.6     3.705   0        0  0
2009.12.9   3.76    3.575   0        0  0
2009.12.10  3.575   3.79    0        0  0
2009.12.11  3.84    3.84    0        0  0
2009.12.14  3.85    3.81    0        0  0
2009.12.15  3.84    3.985   0        0  0
2009.12.16  3.985   4.1     0        0  0
2009.12.17  4.105   4.165   0        0  0
2009.12.18  4.22    4.15    0        0  0
2009.12.21  4.145   4.5     0        0  0
2009.12.22  4.55    4.76    0        0  0
2009.12.23  4.705   4.72    0        0  0
2009.12.24  4.9     4.78    0        0  0
  1. 数据是股票价格,我发出了买入和卖出信号,Cut_Off_Price 是计算买入价的某个百分比。 随着时间的推移,如果第一天的价格低于截止价,我想发出卖出信号。和销售日期。

我想要的结果如下

Date    Open    Close   Buying  Selling Cut_Off_Price   Cut_Off_Signal  Sell_Out    Sell_Out_Date
2009.11.11  4.805   4.43    0       0   0   0   0       0
2009.11.12  4.510   4.505   0       0   0   0   0       0
2009.11.13  4.545   4.765   1       0   3.812   0   1       2009.12.1
2009.11.16  4.780   4.760   0       0   0   0   0       0
2009.11.17  4.755   4.605   0       0   0   0   0       0
2009.11.18  4.560   4.495   0       0   0   0   0       0
2009.11.19  4.495   4.535   1       0   3.628   0   1       2009.12.7
2009.11.20  4.535   4.630   0       0   0   0   0       0
2009.11.23  4.700   4.670   0       0   0   0   0       0
2009.11.24  4.740   4.910   0       0   0   0   0       0
2009.11.25  4.970   4.870   0       0   0   0   0       0
2009.11.26  4.930   4.970   0       0   0   0   0       0
2009.11.27  5.000   4.940   0       0   0   0   0       0
2009.11.30  4.865   4.860   0       0   0   0   0       0
2009.12.1   4.830   3.855   0       0   0   1   0       0
2009.12.2   4.890   4.890   0       0   0   0   0       0
2009.12.3   4.850   4.710   0       0   0   0   0       0
2009.12.4   4.780   4.760   0       0   0   0   0       0
2009.12.7   3.225   3.565   0       0   0   1   0       0
2009.12.8   3.600   3.705   0       0   0   0   0       0   
2009.12.9   3.760   3.575   0       0   0   0   0       0
2009.12.10  3.575   3.790   0       0   0   0   0       0
2009.12.11  3.840   3.840   0       0   0   0   0       0
2009.12.14  3.850   3.810   0       0   0   0   0       0
2009.12.15  3.840   3.985   0       0   0   0   0       0
2009.12.16  3.985   4.100   0       0   0   0   0       0
2009.12.17  4.105   4.165   0       0   0   0   0       0
2009.12.18  4.220   4.150   0       0   0   0   0       0
2009.12.21  4.145   4.500   0       0   0   0   0       0
2009.12.22  4.550   4.760   0       0   0   0   0       0
2009.12.23  4.705   4.720   0       0   0   0   0       0
2009.12.24  4.900   4.780   0       0   0   0   0       0

我的代码如下

 buying_list = data[data['Buying']==True]
 data['Cut_Off_Signal']=False
 def trading(data):
        for idd in range(len(data.index)):
            for ids in range(len(buying_list.index)):
                if data.index[idd]>buying_list.index[ids]:
                   data['Cut_Off_Signal'][idd] = np.where(buying_list['Cut_Off_Price'][ids]>data['Close'][idd],True,False) 
                   data[buying_list.index[ids],'Sell_Out_Signal'] = np.where(data['Cut_Off_Signal'][idd]== True,True,False)
                   #data.loc[buying_list.index[ids],'Sell_Out_Signal']=True
                else:
                   continue

它没有达到我想要的效果,而且花费了太多时间。 (总行是 970,总buying_list 行是 37。但花了大约 5 分钟。 我尝试 .apply(f) 但失败了(我是初学者 ^^;;) 感谢您的建议!

【问题讨论】:

    标签: python for-loop pandas


    【解决方案1】:

    让 pandas 内部为您循环数据,只专注于提供适当的过滤器链。假设我有下面给出的文件 data.txt

    Date,Open,Close,Buying,Selling,Cut_Off_Price
    2009.11.11,4.805,4.43,0,0,0
    2009.11.12,4.51,4.505,0,0,0
    2009.11.13,4.545,4.765,1,0,5.812
    2009.11.16,4.78,4.76,0,0,0
    2009.11.17,4.755,4.605,0,0,0
    2009.11.18,4.56,4.495,0,0,0
    2009.11.19,4.495,4.535,1,0,3.628
    2009.11.20,4.535,4.63,0,0,0
    2009.11.23,4.7,4.67,0,0,0 
    2009.11.24,4.74,4.91,0,0,0
    

    我可以使用下面的代码在数据中找到Buying设置为1,并且Close大于集合的行集Cut_Off_Price

    import pandas as pd
    
    df = pd.read_csv('data.txt')
    buy_signal = df[df.Buying & (df.Cut_Off_Price != 0) & 
                    (df.Close > df.Cut_Off_Price)]
    print(buy_signal)
    

    结果

             Date   Open  Close  Buying  Selling  Cut_Off_Price
    6  2009.11.19  4.495  4.535       1        0          3.628
    

    如果您想创建一个列,您可以先创建一个结合您的过滤器的掩码,然后添加该列。

    buy_mask = df.Buying & (df.Cut_Off_Price != 0) & (df.Close > df.Cut_Off_Price)
    df['Signal_Buy'] = buy_mask + 0 #add 0 to convert from bool to int
    print(df)
    

    结果

             Date   Open  Close  Buying  Selling  Cut_Off_Price  Signal_Buy
    0  2009.11.11  4.805  4.430       0        0          0.000           0
    1  2009.11.12  4.510  4.505       0        0          0.000           0
    2  2009.11.13  4.545  4.765       1        0          5.812           0
    3  2009.11.16  4.780  4.760       0        0          0.000           0
    4  2009.11.17  4.755  4.605       0        0          0.000           0
    5  2009.11.18  4.560  4.495       0        0          0.000           0
    6  2009.11.19  4.495  4.535       1        0          3.628           1
    7  2009.11.20  4.535  4.630       0        0          0.000           0
    8  2009.11.23  4.700  4.670       0        0          0.000           0
    9  2009.11.24  4.740  4.910       0        0          0.000           0
    

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2020-08-11
      • 2022-01-08
      • 2016-10-13
      • 2017-10-29
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多