Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将时间间隔数据帧缩减为12个月x 24小时聚合值的表格_Python_Pandas_Numpy - Fatal编程技术网

Python 将时间间隔数据帧缩减为12个月x 24小时聚合值的表格

Python 将时间间隔数据帧缩减为12个月x 24小时聚合值的表格,python,pandas,numpy,Python,Pandas,Numpy,我正在处理公用电计量器间隔数据,该数据由时间戳(通常为1小时或15分钟增量)和能耗值(以千瓦或千瓦时为单位)组成。我想快速将带有单个读数的pandas数据框转换为带有平均值、最大值和每月、每小时计数的年度总结 年度总结的格式为12个月x 24小时表格(288个单独单元格),其中每个单元格为该特定月份和小时的所有值的平均值、最大值或计数 为了简单起见,让我们看看计算计数。(根据建议,我可以推断对平均值和最大值执行类似的计算。) 我尝试了一种蛮力方法,按月和小时过滤时间戳(一个288个值的循环),并

我正在处理公用电计量器间隔数据,该数据由时间戳(通常为1小时或15分钟增量)和能耗值(以千瓦或千瓦时为单位)组成。我想快速将带有单个读数的pandas数据框转换为带有平均值、最大值和每月、每小时计数的年度总结

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

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

我尝试了一种蛮力方法,按月和小时过滤时间戳(一个288个值的循环),并将计数制成一个矩阵。然而,这种方法似乎非常缓慢,因为我在20米的范围内进行这些计算。我很好奇是否有更快的方法来实现这个目标

下面是如何格式化间隔数据的示例

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()
此数据的计数输出如下所示。(旁注:有时数据不完整,此表有助于识别。)

您可以使用,然后在必要时,使用根据所需输出删除轴名称

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

我会使用groupby然后取消堆栈:

In [11]: res = df.groupby([df.index.month, df.index.hour])["value"].sum().unstack(0, fill_value=0)

In [12]: res.columns.name = "month"  # or None to suppress

In [13]: res.index.name = "hour"  # or None to suppress

In [14]: res
Out[44]:
month   1    2    3    4    5    6    7    8    9    10   11   12
hour
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

注意:我得到了不同的值,我得到了~120,因为每小时4次乘以每个月的天数(有些月比其他月长).

所以cross_tab在默认情况下进行计数(如果不传递aggfunc?)@AndyHayden yes准确地“计算因子的频率表,除非传递了值数组和聚合函数”啊,很好。很有魅力!仅供参考,我能够用
pd.crosstab(df.index.hour,df.index.month,df.values,aggfunc=aggfunc)计算“平均值”和“最大值”
其中
aggfunc
分别是
np.mean
np.max
。抓得好!我修正了这个问题,以反映正确的预期输出。
In [11]: res = df.groupby([df.index.month, df.index.hour])["value"].sum().unstack(0, fill_value=0)

In [12]: res.columns.name = "month"  # or None to suppress

In [13]: res.index.name = "hour"  # or None to suppress

In [14]: res
Out[44]:
month   1    2    3    4    5    6    7    8    9    10   11   12
hour
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