rv1*_*123 3 python numpy minimum pandas
我在Pandas DataFrame中有一个时间序列,我想为其添加一个(未来)最小值的新列。具体来说,假设我的时间序列中具有以下值:
VAL
1
0
4
3
2
3
4
Run Code Online (Sandbox Code Playgroud)
我想找到“期待”的最小值。例如,前两个值的最小值为0,然后下三个值的最小值为2(不再考虑0,因为即使它是整体最小值,也已经过去了),然后是3,最后是4。
VAL MIN
1 0
0 0
4 2
3 2
2 2
3 3
4 4
Run Code Online (Sandbox Code Playgroud)
有什么想法可以使用Pandas或Numpy有效地做到这一点吗?谢谢!
翻转,使用np.minimum.accumulate,翻转回-
In [252]: df['MIN'] = np.minimum.accumulate(df['VAL'][::-1])[::-1]
In [253]: df
Out[253]:
VAL MIN
0 1 0
1 0 0
2 4 2
3 3 2
4 2 2
5 3 3
6 4 4
Run Code Online (Sandbox Code Playgroud)
使用 cummin
df['MIN'] = df.VAL[::-1].cummin()[::-1]
Run Code Online (Sandbox Code Playgroud)
VAL MIN
0 1 0
1 0 0
2 4 2
3 3 2
4 2 2
5 3 3
6 4 4
Run Code Online (Sandbox Code Playgroud)