gpt4 book ai didi

python - Pandas 以绝对最大值重新采样

转载 作者:行者123 更新时间:2023-12-05 02:06:03 24 4
gpt4 key购买 nike

我有一个 DataFrame(带有日期时间索引),例如

2017-01-01 00:00:00    -8.64
2017-01-01 01:00:00 1.02
2017-01-01 02:00:00 1.03
2017-01-01 03:00:00 0.00
2017-01-01 04:00:00 -1.01
2017-01-01 05:00:00 -3.57
2017-01-01 06:00:00 -4.18
2017-01-01 07:00:00 7.73

我想用绝对最大值将其重新采样到 4Hours,即结果应该是

2017-01-01 00:00:00    -8.64
2017-01-01 04:00:00 7.73

但是我找不到任何方法来做到这一点。我试过 df.resample('4H').max(key=abs)

最佳答案

我们可以传递给apply

df.resample('4h').apply(lambda x : max(x, key = abs))
Out[234]:
2017-01-01 00:00:00 -8.64
2017-01-01 04:00:00 7.73
Freq: 4H, Name: caonima, dtype: float64

关于python - Pandas 以绝对最大值重新采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62996988/

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