每 2 小时对一个数据框的数据进行分组 [英] Groupby every 2 hours data of a dataframe
本文介绍了每 2 小时对一个数据框的数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框:
Time T201FN1ST2010 T201FN1VT2010
1791 2017-12-26 00:00:00 854.69 0.87
1792 2017-12-26 00:20:00 855.76 0.87
1793 2017-12-26 00:40:00 854.87 0.87
1794 2017-12-26 01:00:00 855.51 0.87
1795 2017-12-26 01:20:00 856.35 0.86
1796 2017-12-26 01:40:00 856.13 0.86
1797 2017-12-26 02:00:00 855.84 0.85
1798 2017-12-26 02:20:00 856.58 0.85
1799 2017-12-26 02:40:00 856.37 0.85
1800 2017-12-26 03:00:00 855.35 0.86
1801 2017-12-26 03:20:00 855.68 0.86
1802 2017-12-26 03:40:00 855.45 0.85
1803 2017-12-26 04:00:00 855.50 0.85
1804 2017-12-26 04:20:00 855.84 0.85
1805 2017-12-26 04:40:00 856.47 0.85
1806 2017-12-26 05:00:00 855.29 0.86
1807 2017-12-26 05:20:00 855.45 0.87
1808 2017-12-26 05:40:00 855.80 0.86
1809 2017-12-26 06:00:00 854.93 0.88
我正在尝试使用 groupby 对每两个小时的数据进行分组,但我无法做到.
i am trying to group every two hours of data using groupby but i couldn't able to do it.
我试过这个:
last8.groupby(last8.Time.dt.Timedelta('2H'))
但我收到类似 AttributeError: 'DatetimeProperties' object has no attribute 'Timedelta' 的错误.
But i am getting an error like AttributeError: 'DatetimeProperties' object has no attribute 'Timedelta'.
有人可以建议我正确的方法吗?
can anybody suggest me the coreect way to do it?
任何帮助将不胜感激.
推荐答案
我认为你需要 groupby
+ Grouper
+ 一些聚合函数:
I think you need groupby
+ Grouper
+ some aggregate function:
df = last8.groupby(pd.Grouper(freq='2H', key='Time')).mean()
或者 resample
+ 一些聚合函数:
Or resample
+ some aggregate function:
df = last8.resample('2H', on='Time').mean()
或者使用 groupby
+ 地板
:
Or use groupby
+ floor
:
df = last8.groupby(last8.Time.dt.floor('2H')).mean()
<小时>
print (df)
T201FN1ST2010 T201FN1VT2010
Time
2017-12-26 00:00:00 855.551667 0.866667
2017-12-26 02:00:00 855.878333 0.853333
2017-12-26 04:00:00 855.725000 0.856667
2017-12-26 06:00:00 854.930000 0.880000
这篇关于每 2 小时对一个数据框的数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文