gpt4 book ai didi

python - 在 pandas DataFrame 中查找前 3 天的最低值

转载 作者:行者123 更新时间:2023-12-01 04:37:25 26 4
gpt4 key购买 nike

我对Python很陌生。抱歉,如果我的问题很愚蠢..

我正在尝试查找时间序列中前 3 天的最低值。

例如

             price             low
1993-01-29 43.750000 NaN
1993-02-01 43.968700 NaN
1993-02-02 44.125000 43.750000
1993-02-03 42.375000 42.375000
1993-02-04 44.468700 42.375000

我尝试了 .shift()、.min() 等,但都不起作用。非常感谢您的帮助!

最佳答案

无需手动执行此操作,已经有一个函数:

pd.rolling_min( df['price'], 3 )

1993-01-29 NaN
1993-02-01 NaN
1993-02-02 43.750
1993-02-03 42.375
1993-02-04 42.375

更一般地说,有许多滚动式函数可以处理常见情况,还有一个用于用户函数的 rolling_apply。许多库/包都具有此类功能,您通常可以通过搜索“移动”或“滚动”找到这些功能。

Documentaion for pandas moving/rolling functions

关于python - 在 pandas DataFrame 中查找前 3 天的最低值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31489668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com