python - 如何汇总每 5 秒的每一列时间序列数据

标签 python pandas datetime resampling timedelta

我是 pandas 新手,想要修改下面的数据透视表。

beacon_name 6   12  16  23  24
detected_at                                                     
08:00:00    -86.500000  -150.000000 -150    -86.500000  -85.000000  
08:00:01    -78.500000  -150.000000 -150    -88.000000  -88.000000
08:00:02    -82.750000  -150.000000 -150    -81.500000  -87.333333
08:00:03    -83.333333  -150.000000 -150    -81.000000  -83.000000
08:00:04    -87.000000  -150.000000 -150    -86.400000  -84.000000
08:00:05    -88.333333  -150.000000 -150    -79.333333  -85.000000
08:00:06    -90.000000  -150.000000 -150    -85.750000  -86.750000
08:00:07    -150.000000 -150.000000 -150    -84.000000  -88.333333...

具体来说,我想将每 5 个数据行汇总到下面的数据透视表中。

beacon_name 6   12  16  23  24
detected_at                                                     
08:00:00     -418.05     -750     -750     -423.4     -427.3
08:00:05     -688.33     -750     -750     -417.22    -426.5

我搜索并尝试了多种方法,但未能获得正确的输出。

最佳答案

我相信你需要resample ,但首先将时间转换为 timedeltas to_timedelta :

df.index = pd.to_timedelta(df.index)
df = df.resample('5S').sum()
print (df)
                 6     12   16     23     24
beacon_name                                 
08:00:00    -418.1 -750.0 -750 -423.4 -427.3
08:00:05    -328.3 -450.0 -450 -249.1 -260.1

关于python - 如何汇总每 5 秒的每一列时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48264926/

相关文章:

python - 添加 Pandas 系列列表

python - Pandas groupby 操作

c++ - 如何使用 C++ 在 Lotus Notes 的文档中设置特定的日期时间(创建或修改)?

python - 同时按日期和ID分组 Pandas 数据框

python - 如何根据 UTC 的偏移(毫秒?

python - 在 matplotlib 中拆分图例

python - 将基本变量列表写入文本文件

python - 向 pandas 绘图添加小刻度

python - 即使我在每次迭代结束时删除所有变量,python for 循环中也会出现内存泄漏

python - Google的App Engine SDK和Cloud SDK有什么关系?