计算pandas数据框中值的n天高/低排名

问题描述 投票:0回答:2

这是我的数据集,其中索引为日期和价格列。我想在这里创建一个列参数(param),如下评论栏中所示:

 Index          Price   |   param            Comments (P is Price)
1989-01-24      68.800      0               P <  P-1 (P-1 doesnt exist so 0)
                                            param = 0 , 

1989-01-25      68.620     -2               P < P-1 check P<P-2(P-2 doesnt 
                                            exist so P is a 2 day low and 
                                            param = -2 

1989-01-26      68.930      3               P > P-1, P>P-2, P-3(doesnt exist
                                            So P is a 3 day high, param =3 


1989-01-27      68.9900     4               P > P-1 > P-2 > P -3 and hence a 
                                            4 day high, param = 4                                            

1989-01-30      69.11       5               P > P-1> P-2 > P-3 > P-4 and 
                                            hence a 5 day high, param = 5

1989-01-31      69.070     -2               P < P-1 > P-2 and hence a 2 day 
                                            low, param = -2 

有人可以告诉我一种在 pandas 中实现这一目标的优雅方法吗?

python pandas numpy time-series
2个回答
1
投票

您在这里搜索的内容,根据您的评论部分,我的理解是

param
列实际上是我们获得
Price
列的值的排名。这类似于在作为输入的整数流中查找特定值的排名。这可以使用 PriorityQueue 来实现。您需要创建一个带有比较器的优先级队列,该比较器将按其值的升序将元素存储在优先级队列中。为了找到排名,您只需要迭代队列并找到列中最新元素的索引。然而,这将花费 O(n) 时间来查找元素索引。检查下面的 python 文档,了解如何在 python 中创建 heapq 或优先级队列:

Python 中的 HeapQ

如果你想在 O(logn) 内完成此操作,你可以使用自平衡 BST,如 AVL 或红黑树。最近输入的元素的值的排名将是其从左侧开始的索引。在最坏的情况下,这可以在 O(logn) 时间内完成。 python 中 AVL 的详细信息:

使用python的AVL树


0
投票

你想要的是用偏移量进行分组和排名。 Pandas 包含了所有这些。

这里有一个有效的线性解决方案:

df=pd.DataFrame({'price':rand(15)})
df['ascending']=df.price<df.price.shift()
df['slope']=(-1)**df.ascending
df['group']=df.ascending.diff().abs().cumsum()
df['pseudorank']=df.slope.cumsum()
offset=df.groupby('group',sort=False).pseudorank.first()
df['param']=(df.pseudorank-df.join(offset,'group',lsuffix='old').pseudorank+2*df.slope)
df.param=df.param.fillna(0).astype(int)

对于:

       price ascending  slope group  pseudorank  param
0   0.160806     False      1   NaN           1      0
1   0.068664      True     -1     1           0     -2
2   0.663227     False      1     2           1      2
3   0.273134      True     -1     3           0     -2
4   0.610329     False      1     4           1      2
5   0.595016      True     -1     5           0     -2
6   0.975163     False      1     6           1      2
7   0.692874      True     -1     7           0     -2
8   0.682642      True     -1     7          -1     -3
9   0.337418      True     -1     7          -2     -4
10  0.307546      True     -1     7          -3     -5
11  0.462594     False      1     8          -2      2
12  0.304216      True     -1     9          -3     -2
13  0.189434      True     -1     9          -4     -3
14  0.865468     False      1    10          -3      2

我创建了很多列来进行解释,如果你愿意,你可以删除它们。

© www.soinside.com 2019 - 2024. All rights reserved.