使用python获得一小时内进行的许多观察结果,以获取平均每小时数据帧(对于每日数据)
我有以下示例数据:
DATE TIMEREAD LEVEL
09/07/2013 12:30:12 5
09/07/2013 12:40:07 2
09/07/2013 12:50:09 5
10/07/2013 01:05:58 3
10/07/2013 01:25:15 2
11/07/2013 15:05:10 1
我想每天获得平均值。这是我所需的输出:
DATE TIMEREAD LEVEL
09/07/2013 12:00:00 4
10/07/2013 01:00:00 2.5
11/07/2013 15:00:00 1
有人可以帮助我吗?
我使用了这个:
dates = pd.date_range('2013-07-9', '2018-12-31', freq = 'H')
df['date'] = dates.date
df['hour'] = dates.hour
df['day'] = dates.dayofweek
df= .groupby(df['date', 'hour','day'].apply(lambda x: x.mean()))
但是我没有得到所需的输出,看来它与所需的小时数(每天24小时)与数据框中的实际数据匹配(因为有些小时没有“级别”的数据)。
I have the following sample data:
DATE TIMEREAD LEVEL
09/07/2013 12:30:12 5
09/07/2013 12:40:07 2
09/07/2013 12:50:09 5
10/07/2013 01:05:58 3
10/07/2013 01:25:15 2
11/07/2013 15:05:10 1
I want to get the mean value per hour each day. This is my desired output:
DATE TIMEREAD LEVEL
09/07/2013 12:00:00 4
10/07/2013 01:00:00 2.5
11/07/2013 15:00:00 1
Does anyone could help me?
I used this:
dates = pd.date_range('2013-07-9', '2018-12-31', freq = 'H')
df['date'] = dates.date
df['hour'] = dates.hour
df['day'] = dates.dayofweek
df= .groupby(df['date', 'hour','day'].apply(lambda x: x.mean()))
but I don't get the desired output and it seems it doesnt match the desired number of hours (24hrs per day) to the actual data in the dataframe (because some hours have no data for "LEVEL").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将每个时间戳舍入到最近的小时,然后使用
.groupby()
和.mean()
生成所需的输出:此输出:
Round down each timestamp to the nearest hour, then use
.groupby()
and.mean()
to generate the desired output:This outputs: