熊猫:滚动第二大价值

fre*_*888 3 python pandas rolling-computation

我需要获得 df 的滚动第二大值。

为了获得最大的价值我所做的

max = df.sort_index(ascending=True).rolling(10).max()
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,python 抛出一个错误

max = df.sort_index(ascending=True).rolling(10).nlargest(2)

AttributeError: 'Rolling' object has no attribute 'nlargest'
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?我还能使用什么高性能的东西?

Ita*_*kin 6

我会做这样的事情:

df.rolling(10).apply(lambda x: pd.Series(x).nlargest(2).iloc[-1])
Run Code Online (Sandbox Code Playgroud)