scl*_*cls 3 python min accumulate pandas cumulative-min
我正在寻找一种使用Python Pandas在没有窗口的情况下滚动(*)分钟的方法。
让我们考虑以下 Series
In [26]: s = pd.Series([10, 12, 14, 9, 10, 8, 16, 20])
Out[26]:
0 10
1 12
2 14
3 9
4 10
5 8
6 16
7 20
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想要一个像
0 10
1 10
2 10
3 9
4 9
5 8
6 8
7 8
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我试过了
s.rolling().min()
Run Code Online (Sandbox Code Playgroud)
但出现以下错误
TypeError: rolling() missing 1 required positional argument: 'window'
Run Code Online (Sandbox Code Playgroud)
我做了这个
r = s.copy()
val_min = r.iloc[0]
for i, (idx, val) in enumerate(r.iteritems()):
if i > 0:
if val < val_min:
val_min = val
else:
r[idx] = val_min
Run Code Online (Sandbox Code Playgroud)
并有正确答案
In [30]: r
Out[30]:
0 10
1 10
2 10
3 9
4 9
5 8
6 8
7 8
dtype: int64
Run Code Online (Sandbox Code Playgroud)
但我认为Pandas方法可能应该存在(并且效率要高得多),或者如果不存在,就应该实现。
(*)“滚动”可能不是一个合适的术语,也许应将其命名为“本地”最小值。
编辑:实际上是命名为累积最小值或扩展最小值
print(s.cummin())
0 10
1 10
2 10
3 9
4 9
5 8
6 8
7 8
dtype: int64
Run Code Online (Sandbox Code Playgroud)