python - 将 pandas 间隔数据框减少为 12 个月 x 24 小时聚合值的表

标签 python pandas numpy

我正在处理公用事业仪表间隔数据,其中包含时间戳(通常以 1 小时或 15 分钟为增量)和能耗值(以 kW 或 kWh 为单位)。我想快速将包含单独读数的 pandas 数据框转换为包含平均值、最大值以及每月、每小时计数的年度摘要。

年度摘要的格式将是一个 12 个月 x 24 小时的表格(288 个单独的单元格),其中每个单元格是该特定月份和小时的所有值的平均值、最大值或计数。

为了简单起见,我们只看一下计算计数。(根据建议,我可以推断对平均值和最大值执行类似的计算。)

我尝试了一种强力方法,按月和小时过滤时间戳(288 个值的循环)并将计数制成矩阵。然而,当我在 20 米的距离上执行这些计算时,这种方法似乎非常慢。我很好奇是否有更快的方法来利用 pandas/numpy 实现此目的。

以下是间隔数据格式化方式的示例。

from datetime import datetime
import pandas as pd

df = pd.DataFrame()
df["start"] = pd.date_range(start=datetime(2018, 1, 1), end=datetime(2018, 12, 31, 23), freq='900S')
df["value"] = 1
df.set_index("start", inplace=True)

我目前正在按照以下方式进行计算:

for month in range(1, 13):
    for hour in range(0, 24):
        count = df.query("index.dt.month == {} and index.dt.hour == {}".format(month, hour)).count()

此数据的计数输出如下所示。 (旁注:有时数据不完整,此表可以帮助识别。)

     1    2    3    4    5    6    7    8    9    10   11   12
0   124  112  124  120  124  120  124  124  120  124  120  124
1   124  112  124  120  124  120  124  124  120  124  120  124
2   124  112  124  120  124  120  124  124  120  124  120  124
3   124  112  124  120  124  120  124  124  120  124  120  124
4   124  112  124  120  124  120  124  124  120  124  120  124
5   124  112  124  120  124  120  124  124  120  124  120  124
6   124  112  124  120  124  120  124  124  120  124  120  124
7   124  112  124  120  124  120  124  124  120  124  120  124
8   124  112  124  120  124  120  124  124  120  124  120  124
9   124  112  124  120  124  120  124  124  120  124  120  124
10  124  112  124  120  124  120  124  124  120  124  120  124
11  124  112  124  120  124  120  124  124  120  124  120  124
12  124  112  124  120  124  120  124  124  120  124  120  124
13  124  112  124  120  124  120  124  124  120  124  120  124
14  124  112  124  120  124  120  124  124  120  124  120  124
15  124  112  124  120  124  120  124  124  120  124  120  124
16  124  112  124  120  124  120  124  124  120  124  120  124
17  124  112  124  120  124  120  124  124  120  124  120  124
18  124  112  124  120  124  120  124  124  120  124  120  124
19  124  112  124  120  124  120  124  124  120  124  120  124
20  124  112  124  120  124  120  124  124  120  124  120  124
21  124  112  124  120  124  120  124  124  120  124  120  124
22  124  112  124  120  124  120  124  124  120  124  120  124
23  124  112  124  120  124  120  124  124  120  124  120  124

最佳答案

您可以使用pandas.crosstab ,然后如有必要,请使用 DataFrame.rename_axis根据所需的输出删除轴名称。

df_new = (pd.crosstab(df.index.hour, df.index.month)
          .rename_axis(None)
          .rename_axis(None, axis=1))

[输出]

     1    2    3    4    5    6    7    8    9    10   11   12
0   124  112  124  120  124  120  124  124  120  124  120  124
1   124  112  124  120  124  120  124  124  120  124  120  124
2   124  112  124  120  124  120  124  124  120  124  120  124
3   124  112  124  120  124  120  124  124  120  124  120  124
4   124  112  124  120  124  120  124  124  120  124  120  124
5   124  112  124  120  124  120  124  124  120  124  120  124
6   124  112  124  120  124  120  124  124  120  124  120  124
7   124  112  124  120  124  120  124  124  120  124  120  124
8   124  112  124  120  124  120  124  124  120  124  120  124
9   124  112  124  120  124  120  124  124  120  124  120  124
10  124  112  124  120  124  120  124  124  120  124  120  124
11  124  112  124  120  124  120  124  124  120  124  120  124
12  124  112  124  120  124  120  124  124  120  124  120  124
13  124  112  124  120  124  120  124  124  120  124  120  124
14  124  112  124  120  124  120  124  124  120  124  120  124
15  124  112  124  120  124  120  124  124  120  124  120  124
16  124  112  124  120  124  120  124  124  120  124  120  124
17  124  112  124  120  124  120  124  124  120  124  120  124
18  124  112  124  120  124  120  124  124  120  124  120  124
19  124  112  124  120  124  120  124  124  120  124  120  124
20  124  112  124  120  124  120  124  124  120  124  120  124
21  124  112  124  120  124  120  124  124  120  124  120  124
22  124  112  124  120  124  120  124  124  120  124  120  124
23  124  112  124  120  124  120  124  124  120  124  120  124

关于python - 将 pandas 间隔数据框减少为 12 个月 x 24 小时聚合值的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55579696/

相关文章:

python - 在 pandas 数据框的分组中获得唯一计数和最大值

python - Google Analytics 对 Python 中 Pandas Dataframe 的响应

python - python 中的灰化矩阵,包括 NaN

python - 将列标题转换为 Python 中的值和相应计数

Python 和 Pandas : UnicodeDecodeError: 'ascii' codec can't decode byte

pandas - Jupyter Notebook 仍然截断 pandas 列

python - 为什么 'tornado.ioloop.IOLoop.instance().start()' 给我一个错误?

Python - 打字 - 可订阅类型的联合

python - Splinter 拯救无形的 html

python删除以 '\u...'开头的单词