【问题标题】:efficient way to find several rows above and below a subset of data查找数据子集上方和下方的几行的有效方法
【发布时间】:2014-03-03 15:02:01
【问题描述】:

我想知道是否有一种有效的方法可以在行子集的下方和上方获取 X 行。我在下面创建了一个基本实现,但我确信有更好的方法。我关心的子集是buyindex,它是具有买入信号的行的索引。我想在 sellindex 上方和下方获取几行,以验证我的算法是否正常工作。我如何以有效的方式做到这一点?我的方式似乎是迂回的。

buyindex = list(data2[data2['buy'] == True].index)

print buyindex [71, 102, 103, 179, 505, 506, 607]

buyindex1 = map(lambda x: x + 1, buyindex)
buyindex2 = map(lambda x: x - 1, buyindex)
buyindex3 = map(lambda x: x - 2, buyindex)
buyindex4 = map(lambda x: x + 2, buyindex)
buyindex.extend(buyindex1)
buyindex.extend(buyindex2)
buyindex.extend(buyindex3)
buyindex.extend(buyindex4)

buyindex.sort()
data2.iloc[buyindex]

UPDATE - 这是数据的结构。 我有“买入”指数。但我基本上想获得高于和低于买入的几个指数。

VTI upper   lower   sell    buy AboveUpper  BelowLower  date    tokens_left
38   61.25   64.104107   61.341893   False   True    False   True   2007-02-28 00:00:00  5
39   61.08   64.218341   61.109659   False   True    False   True   2007-03-01 00:00:00  5
40   60.21   64.446719   60.640281   False   True    False   True   2007-03-02 00:00:00  5
41   59.51   64.717936   60.050064   False   True    False   True   2007-03-05 00:00:00  5
142  63.27   68.909776   64.310224   False   True    False   True   2007-07-27 00:00:00  5
217  62.98   68.858308   63.587692   False   True    False   True   2007-11-12 00:00:00  5
254  61.90   66.941126   61.944874   False   True    False   True   2008-01-07 00:00:00  5
255  60.79   67.049925   61.312075   False   True    False   True   2008-01-08 00:00:00  5
296  57.02   61.382677   57.371323   False   True    False   True   2008-03-07 00:00:00  5
297  56.15   61.709166   56.788834   False   True    False   True   2008-03-10 00:00:00  5

更新:我根据所选答案创建了一个通用函数。如果您认为这可以提高效率,请告诉我。

def get_test_index(df, column, numbers):  
    """
    builds an test index based on a range of numbers above and below the a specific index you want.
    df = dataframe to build off of 
    column = the column that is important to you. for instance, 'buy', or 'sell' 
    numbers = how many above and below you want of the important index 

    """

    idx_l = list(df[df[column] == True].index)
    for i in range(numbers)[1:]:
        idxpos = data2[column].shift(i).fillna(False)
        idxpos = list(df[idxpos].index)
        idx_l.extend(idxpos)

        idxneg = data2[column].shift(-i).fillna(False)
        idxneg = list(df[idxneg].index)
        idx_l.extend(idxneg)
    #print idx_l
    return sorted(idx_l)

【问题讨论】:

标签: python pandas stocks


【解决方案1】:

这将是一个非常有效的方法

In [39]: df = DataFrame(np.random.randn(10,2))

In [41]: start=3

In [42]: stop=4

In [43]: df.iloc[(max(df.index.get_loc(start)-2,0)):min(df.index.get_loc(stop)+2,len(df))]
Out[43]: 
          0         1
1  0.348326  1.413770
2  1.898784  0.053780
3  0.825941 -1.986920
4  0.075956 -0.324657
5 -2.736800 -0.075813

[5 rows x 2 columns]

如果您本质上想要一个任意索引器的功能,只需创建一个列表 你想要的并传递给.iloc

In [18]: index_wanted = [71, 102, 103, 179, 505, 506, 607]

In [19]: from itertools import chain

In [20]: df = DataFrame(np.random.randn(1000,2))

你可能想要独特的

f = lambda i: [ i-2, i-1, i, i+1, i+2 ]

In [21]: indexers = Index(list(chain(*[ f(i) for i in [71, 102, 103, 179, 505, 506, 607] ]))).unique()

In [22]: df.iloc[indexers]
Out[22]: 
            0         1
69   0.792996  0.264597
70   1.084315 -0.620006
71  -0.030432  1.219576
72  -0.767855  0.765041
73  -0.637771 -0.103378
100 -1.087505  1.698133
101  1.007143  2.594046
102 -0.307440  0.308360
103  0.944429 -0.411742
104  1.332445 -0.149350
105  0.165213  1.125668
177  0.409580 -0.375709
178 -1.757021 -0.266762
179  0.736809 -1.286848
180  1.856241  0.176931
181 -0.492590  0.083519
503 -0.651788  0.717922
504 -1.612517 -1.729867
505 -1.786807 -0.066421
506  1.423571  0.768161
507  0.186871  1.162447
508  1.233441 -0.028261
605 -0.060117 -1.459827
606 -0.541765 -0.350981
607 -1.166172 -0.026404
608 -0.045338  1.641864
609 -0.337748  0.955940

[27 rows x 2 columns]

【讨论】:

  • 是否可以轻松地将其推广到 20 个单位长的索引?我认为这段代码真的很干净。但是,如果我的索引在 1000 行长的数据框中包含 20 个不同的数字,则不清楚如何根据 sellindex ([71, 102, 103, 179, 505, 506 , 607])
  • 已更新...当然我误读了您的问题,只是认为您想要一个。它很容易概括。
  • 谢谢杰夫。这看起来真的很好。我在这里阅读有关链的信息:bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/… 你能帮我理解为什么这里使用链而不是 zip 吗?
  • 再抱歉:如果我想创建一个指定我想要的范围的函数,我该如何调整下面的行,这样我就不会每次都更改它?因此,例如,如果我想要上下 3 或 4 个。 indexers = Index(list(chain(*[ [i-2,i-1,i,i,i+1,i+2] for i in [71, 102, 103, 179, 505, 506, 607] ] ))).unique()
  • 我更新了向您展示如何使用函数。链所做的所有事情都是将列表放在一起(基本上将列表列表扁平化为单个列表)
【解决方案2】:

您可以使用shift| 运算符;例如 +/- 2 天,您可以这样做

idx = (data2['buy'] == True).fillna(False)
idx |= idx.shift(-1) | idx.shift(-2)   # one & two days after
idx |= idx.shift(1) | idx.shift(2)     # one & two days before
data2[ idx ] # this is what you need  

【讨论】:

  • 谢谢贝扎德。你能再充实一点吗?我不断收到此错误: TypeError: unsupported operand type(s) for |: 'bool' and 'float'for ----> 3 idx |= data2.buy.shift(1) | data2.buy.shift(2)
  • 你能帮我理解一下吗? (这叫管道吗?)在这里做什么?我正在阅读此链接,但我并不完全清楚:stackoverflow.com/questions/5988665/pipe-character-in-python
  • **对不起所有的 cmets。我想我现在明白了。 Pipe 的作用类似于“或”,它允许您组合多个符合条件的索引。如果你使用“&”,你会得到一个空的,因为很难找到适合所有条件的 df 部分?
  • 但是,您的第二行 "idx |= data2.buy.shift(1) | data2.buy.shift(2)" 似乎仍然对我不起作用...
  • @user3314418 | 这里基本上是成对的或;请尝试我编辑的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 2018-01-19
  • 1970-01-01
相关资源
最近更新 更多