我有以下pandas DataFrame df
:
DateTime SENSOR PROD
2019-04-01 00:00:00 0 0.0
2019-04-01 00:30:00 1 10.0
2019-04-01 01:00:00 1 5.0
2019-04-01 01:30:00 1 1.0
2019-04-01 02:00:00 1 12.0
2019-04-01 02:30:00 0 0.0
字符串
这些值是以半小时的粒度给出的。我应该以小时的粒度对PROD
求和,得到以下结果。
DateTime PROD
2019-04-01 00:00:00 10.0
2019-04-01 01:00:00 6.0
2019-04-01 02:00:00 12.0
型
4条答案
按热度按时间4ioopgfo1#
你可以这样使用
DataFrame.resample
:字符串
要使用
resample
,你需要确保你的索引是DatetimeIndex
。我认为有几种方法可以做到这一点,但你可以尝试:型
cgvd09ve2#
可以使用
resample
:个字符
注意:如果您的
DateTime
列还不是datetime格式,请先执行以下操作:型
gkn4icbw3#
您需要使用pd.Grouper
字符串
输出量:
型
daolsyd04#
resample
支持on
选项来指定用于重采样而不是索引的列。因此,您不需要将_index设置为datetimeindex
。只需在on
中指定列名即可字符串
另一种方法是使用
groupby
将df.DateTime
向下转换为datetime64[h]
型