Pandas通过FixedForwardWindowIndexer获得滚动排名

yuvru6vn  于 2022-12-21  发布在  其他
关注(0)|答案(1)|浏览(155)

我使用的是Pandas 1.51,我试图通过使用FixedForwardWindowIndexer来获得滚动窗口中 Dataframe 中每一行的排名,但我无法理解结果。

df = pd.DataFrame({"X":[9,3,4,5,1,2,8,7,6,10,11]})
window_size = 5
indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=window_size)
df.rolling(window=indexer).rank(ascending=False)

结果:

X
0   5.0
1   4.0
2   1.0
3   2.0
4   3.0
5   1.0
6   1.0
7   NaN
8   NaN
9   NaN
10  NaN

据我估计,它应该看起来像:

X
0   1.0 # based on the window [9,3,4,5,1], 9 is ranked 1st w/ascending = False
1   3.0 # based on the window [3,4,5,1,2], 3 is ranked 3rd
2   3.0 # based on the window [4,5,1,2,8], 4 is ranked 3rd
3   3.0 # etc
4   5.0
5   5.0
6   3.0
7   NaN
8   NaN
9   NaN
10  NaN

我是基于一个向后看的窗口,它工作得很好:

>>> df.rolling(window_size).rank(ascending=False)
      X
0   NaN
1   NaN
2   NaN
3   NaN
4   5.0
5   4.0
6   1.0
7   2.0
8   3.0
9   1.0
10  1.0

任何帮助都是最受欢迎的。

hi3rlvi2

hi3rlvi21#

下面是另一种方法:

df["rank"] = [
    x.rank(ascending=False).iloc[0].values[0]
    for x in df.rolling(window_size)
    if len(x) == window_size
] + [pd.NA] * (window_size - 1)

然后:

print(df)
# Output
     X  rank
0    9   1.0
1    3   3.0
2    4   3.0
3    5   3.0
4    1   5.0
5    2   5.0
6    8   3.0
7    7  <NA>
8    6  <NA>
9   10  <NA>
10  11  <NA>

相关问题