gpt4 book ai didi

python - Pandas .resample() 方法的 R 等价物是什么?

转载 作者:太空狗 更新时间:2023-10-29 23:58:31 24 4
gpt4 key购买 nike

这是我找到的最接近的链接:https://stats.stackexchange.com/questions/5305/how-to-re-sample-an-xts-time-series-in-r

但我没有看到任何关于您可以在 pandas 中聚合数据的不同方法(例如平均值、计数、匿名函数)。

对于我的程序,我试图让数据帧每 2 分钟重新采样一次,并在每个时间间隔取 2 个值的平均值。谢谢!

最佳答案

如果你使用 data.tablelubridate 它可能看起来像这样

library(data.table)
library(lubridate)
#sample data
dt<-data.table(ts=seq(from=ymd('2015-01-01'), to=ymd('2015-07-01'),by='mins'), datum=runif(260641,0,100))

如果你想获取每分钟到每小时的数据,你可以这样做

 dt[,mean(datum),by=floor_date(ts,"hour")]

如果你有一堆列并且你希望对所有列进行平均你可以这样做

dt[,lapply(.SD,mean),by=floor_date(ts,"hour")]

您可以将 mean 替换为您喜欢的任何函数。您可以将“小时”替换为“秒”、“分钟”、“小时”、“日”、“周”、“月”、“年”。好吧,你不能从分钟到秒,因为这需要魔法,但无论如何你可以从微秒到秒。

It is not possible to convert a series from a lower periodicity to a higher periodicity - e.g. weekly to daily or daily to 5 minute bars, as that would require magic.

-xts 手册中的 Jeffrey Ryan。

我从来没有学过 xts,所以我不知道用 xts 对象来做的语法,但是那行很有名(或者至少和手册中的行一样有名)

关于python - Pandas .resample() 方法的 R 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616873/

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