Python,Pandas - 将函数应用于每个特定项目

时间:2017-06-10 17:31:21

标签: python pandas

请帮助新手) 我想草拟一个功能,可以应用于系列中的每个特定项目。想法如下:功能查看每个特定项目并将其与另一行和另一个系列中的项目进行比较;如果满足功能条件,则条件作为新项目返回到新系列;在该功能进入应用系列的下一个项目之后。

基于我的简单知识,我有以下功能,但它不起作用(内核只是忙着没有任何结果):

def trail_stop(high, low, close):
  for row in close:
    i = -1
    run = True  
    high.s = high.shift(i)
    low.s = low.shift(i)
    trail = (close + 0.0002)
    stop_loss = (close - 0.0002)
    while run == True:
      if high.s[0] > trail and low.s[0] < stop_loss:
        stop_loss = trail
        trail = trail + 0.0002
        i = i - 1
      elif low.s[0] > stop_loss:
        return stop_loss
        run = False
      elif high.s[0] < trail and low.s[0] < stop_loss:
        i = i - 1
      else:
        run = False

table['trail'] = table.apply(trail_stop(table.High, table.Low, table.Close), axis = 1) 

在实际输入数据(关闭,高,低)上我想收到以下系列作为输出(跟踪):

        High      Low    Close    trail
0    1.32396  1.32358  1.32391  1.32371
1    1.32392  1.32365  1.32365  1.32385 
2    1.32370  1.32364  1.32369  1.32389 
3    1.32378  1.32365  1.32371  1.32391 
4    1.32378  1.32360  1.32360  1.32380 
5    1.32390  1.32366  1.32370  1.32390 
6    1.32384  1.32370  1.32384  1.32364 
7    1.32386  1.32355  1.32380  1.32360 
8    1.32384  1.32358  1.32379  None
9    1.32389  1.32379  1.32387  None 
10   1.32386  1.32379  1.32383  1.32363 
11   1.32394  1.32360  1.32387  None 
12   1.32389  1.32370  1.32370  1.32390  
13   1.32390  1.32370  1.32390  1.32370 
14   1.32390  1.32364  1.32387  None  
15   1.32382  1.32373  1.32382  None 

我对此事的任何建议表示赞赏)

更新:输入数据的其他示例:

   High     Low   Close
1.32396 1.32391 1.32394
1.32397 1.32391 1.32396
1.32396 1.32391 1.32396
1.32397 1.32394 1.32396
1.32398 1.32393 1.32393
1.32396 1.32392 1.32396
1.32396 1.32390 1.32396
1.32398 1.32390 1.32396
1.32398 1.32391 1.32396
1.32398 1.32385 1.32397
1.32397 1.32387 1.32395
1.32433 1.32398 1.32409
1.32417 1.32404 1.32417
1.32418 1.32406 1.32413
1.32436 1.32412 1.32436
1.32453 1.32429 1.32451
1.32454 1.32451 1.32453
1.32458 1.32447 1.32452
1.32454 1.32416 1.32424
1.32449 1.32419 1.32449
1.32451 1.32421 1.32438

0 个答案:

没有答案