【问题标题】:Python Pandas: Aggregate data by hour and display it instead of the indexPython Pandas:按小时聚合数据并显示它而不是索引
【发布时间】:2023-03-03 10:14:02
【问题描述】:

我想使用 pandas 按小时聚合一些数据并显示日期而不是索引。

我现在的代码如下:

import pandas as pd
import numpy as np

dates = pd.date_range('1/1/2011', periods=20, freq='25min')
data = pd.Series(np.random.randint(100, size=20), index=dates)

result = data.groupby(data.index.hour).sum().reset_index(name='Sum')

print(result)

显示的内容大致如下:

   index  Sum
0      0  131
1      1  116
2      2  180
3      3   62
4      4   95
5      5  107
6      6   89
7      7  169

问题是我想显示与那个小时相关的日期而不是索引。

我想要达到的结果如下:

       index                Sum
0      2011-01-01 01:00:00  131
1      2011-01-01 02:00:00  116
2      2011-01-01 03:00:00  180
3      2011-01-01 04:00:00   62
4      2011-01-01 05:00:00   95
5      2011-01-01 06:00:00  107
6      2011-01-01 07:00:00   89
7      2011-01-01 08:00:00  169

有什么方法可以让我使用 pandas 轻松做到这一点?

【问题讨论】:

    标签: python pandas date numpy


    【解决方案1】:

    你可以使用resample

    data.resample('H').sum()
    

    输出:

    2011-01-01 00:00:00     84
    2011-01-01 01:00:00    121
    2011-01-01 02:00:00    160
    2011-01-01 03:00:00     70
    2011-01-01 04:00:00     88
    2011-01-01 05:00:00    131
    2011-01-01 06:00:00     56
    2011-01-01 07:00:00    109
    Freq: H, dtype: int32
    

    选项#2

    data.groupby(data.index.floor('H')).sum()
    

    输出:

    2011-01-01 00:00:00     84
    2011-01-01 01:00:00    121
    2011-01-01 02:00:00    160
    2011-01-01 03:00:00     70
    2011-01-01 04:00:00     88
    2011-01-01 05:00:00    131
    2011-01-01 06:00:00     56
    2011-01-01 07:00:00    109
    dtype: int32
    

    【讨论】:

    • 太棒了。非常感谢!
    【解决方案2】:
    data.groupby(data.index.strftime('%Y-%m-%d %H:00:00')).sum().reset_index(name='Sum')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2021-05-27
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多