Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 重新采样数据以添加缺少的小时值_Python_Pandas_Datetime - Fatal编程技术网

Python 重新采样数据以添加缺少的小时值

Python 重新采样数据以添加缺少的小时值,python,pandas,datetime,Python,Pandas,Datetime,我正在使用的df如下所示: trans_id amount month day hour 2018-08-18 12:59:59+00:00 1 46 8 18 12 2018-08-26 01:56:55+00:00 2 20 8 26 1 我打算获得每小时的平均“金额”。我使用以下代码来实现这一点: df2 = df.gro

我正在使用的df如下所示:

                        trans_id    amount  month   day     hour
2018-08-18 12:59:59+00:00   1         46    8       18       12
2018-08-26 01:56:55+00:00   2         20    8       26       1
我打算获得每小时的平均“金额”。我使用以下代码来实现这一点:

df2 = df.groupby(['month', 'day', 'day_name', 'hour'], as_index = False)['amount'].sum()
这给了我每个月的总金额-天-天-小时组合,这是确定的。但当我计算每天的总小时数时,它们并不像预期的那样都是24小时。我认为,由于某些事务在特定时间(月-日-日-小时)不存在的事实

我的问题是,如果他们有记录或没有记录,我如何让所有的24小时不相关

谢谢

配合使用:

df2 = (df.groupby(['month', 'day', 'day_name', 'hour'])['amount']
         .sum()
         .unstack(fill_value=0)
         .stack()
         .reset_index())