【问题标题】:How to aggregate data hourly, weekly and monthly in pandas如何在 pandas 中每小时、每周和每月汇总数据
【发布时间】:2018-04-24 10:36:50
【问题描述】:

我有一个名为“Red”的 python pandas 数据框,其中包含两列 TimeStamp 和 Red。索引已设置为 TimeStamp。 Sum() 已应用,但它基于秒聚合。我需要以每小时、每周和每月为基础进行汇总。请指导,谢谢

In [56]: Red.columns 
Out[56]: Index(['TimeStamp', 'Red'], dtype='object') 
In [64]: Red.shape , type(Red) 
Out[64]: ((1381701, 2), pandas.core.frame.DataFrame) 
In [69]: Red.head(5) 
Out[69]: 
       TimeStamp      Red 
0 2017-05-01 00:00:01  1  
1 2017-05-01 00:00:01  1 
2 2017-05-01 00:00:01  1 
3 2017-05-01 00:00:01  1 
4 2017-05-01 00:00:01  1 
In [70]: Red.groupby('TimeStamp').sumfthead(3)
Out[70]:  
      TimeStamp      Red
2017-05-01 00:00:01  16 
2017-05-01 00:00:02  16 
2017-05-01 00:00:03  16  

【问题讨论】:

标签: python pandas


【解决方案1】:

看起来你应该使用 Series 方法 .resample() 一旦你得到一个 TimeSeriesStamp 作为索引。

查看文档以获取更多详细信息。 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.resample.html

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多