在Python中使用Pandas查找每日最大小时数 [英] Finding hour of daily max using Pandas in Python

查看:123
本文介绍了在Python中使用Pandas查找每日最大小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在需求时间序列中找到每天的最大需求小时.

I am trying to find the hour of max demand every day in my demand time series.

我创建了一个看起来像..p的数据框.

I have created a dataframe that looks like..

                       power
2011-01-01 00:00:00  1015.70
2011-01-01 01:00:00  1015.70
2011-01-01 02:00:00  1010.30
2011-01-01 03:00:00  1010.90
2011-01-01 04:00:00  1021.10
2011-01-01 05:00:00  1046.00
2011-01-01 06:00:00  1054.60
...

和一组分组的序列,以使用.max()找出每天的最大值

and a grouped series to find the max value from each day using .max()

grouped = df.groupby(pd.TimeGrouper('D'))
grouped['power'].max()

输出

2011-01-01    1367.30
2011-01-02    1381.90
2011-01-03    1289.00
2011-01-04    1323.50
2011-01-05    1372.70
2011-01-06    1314.40
2011-01-07    1310.60
...

但是我也需要小时的最大值.像这样:

However I need the hour of the max value also. So something like:

2011-01-01  18  1367.30
2011-01-02  5   1381.90
2011-01-03  22  1289.00
2011-01-04  10  1323.50
...

我尝试使用idxmax(),但我不断收到ValueError

I have tried using idxmax() but I keep getting a ValueError

推荐答案

自2018-09-19起更新:

FutureWarning:不建议使用pd.TimeGrouper并将其删除; 请使用pd.Grouper(freq = ...)

FutureWarning: pd.TimeGrouper is deprecated and will be removed; Please use pd.Grouper(freq=...)

解决方案:

In [295]: df.loc[df.groupby(pd.Grouper(freq='D')).idxmax().iloc[:, 0]]
Out[295]:
                                         power
2011-01-01 06:00:00                     1054.6
2011-01-02 06:00:00                     2054.6


旧答案:

尝试一下:

In [376]: df.loc[df.groupby(pd.TimeGrouper('D')).idxmax().iloc[:, 0]]
Out[376]:
                                           power
2011-01-01 06:00:00                       1054.6
2011-01-02 06:00:00                       2054.6

数据:

In [377]: df
Out[377]:
                                           power
2011-01-01 00:00:00                       1015.7
2011-01-01 01:00:00                       1015.7
2011-01-01 02:00:00                       1010.3
2011-01-01 03:00:00                       1010.9
2011-01-01 04:00:00                       1021.1
2011-01-01 05:00:00                       1046.0
2011-01-01 06:00:00                       1054.6
2011-01-02 00:00:00                       2015.7
2011-01-02 01:00:00                       2015.7
2011-01-02 02:00:00                       2010.3
2011-01-02 03:00:00                       2010.9
2011-01-02 04:00:00                       2021.1
2011-01-02 05:00:00                       2046.0
2011-01-02 06:00:00                       2054.6

这篇关于在Python中使用Pandas查找每日最大小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆