按分钟和计算平均指数组(Group index by minute and compute aver

2019-09-28 15:10发布

所以,我有所谓的“DF”一个熊猫数据框,我想删除秒,只是在YYYY-MM-DD HH指数:MM格式。 但也分钟,然后进行分组,并显示在该分钟内的平均值。

所以,我希望把这个数据帧

                        value
2015-05-03 00:00:00     61.0
2015-05-03 00:00:10     60.0
2015-05-03 00:00:25     60.0
2015-05-03 00:00:30     61.0
2015-05-03 00:00:45     61.0
2015-05-03 00:01:00     61.0
2015-05-03 00:01:10     60.0
2015-05-03 00:01:25     60.0
2015-05-03 00:01:30     61.0
2015-05-03 00:01:45     61.0
2015-05-03 00:02:00     61.0
2015-05-03 00:02:10     60.0
2015-05-03 00:02:25     60.0
2015-05-03 00:02:40     60.0
2015-05-03 00:02:55     60.0
2015-05-03 00:03:00     59.0
2015-05-03 00:03:15     59.0
2015-05-03 00:03:20     59.0
2015-05-03 00:03:35     59.0
2015-05-03 00:03:40     60.0

这个数据帧

                        value
2015-05-03 00:00        60.6
2015-05-03 00:01        60.6
2015-05-03 00:02        60.2
2015-05-03 00:03        59.2

香港专业教育学院尝试这样的代码

df['value'].resample('1Min').mean()

要么

df.index.resample('1Min').mean()

但这似乎并没有工作。 有任何想法吗?

Answer 1:

你需要先转换指数DatetimeIndex

df.index = pd.DatetimeIndex(df.index)
#another solution
#df.index = pd.to_datetime(df.index)

print (df['value'].resample('1Min').mean())
#another same solution
#print (df.resample('1Min')['value'].mean())
2015-05-03 00:00:00    60.6
2015-05-03 00:01:00    60.6
2015-05-03 00:02:00    60.2
2015-05-03 00:03:00    59.2
Freq: T, Name: value, dtype: float64

在索引塞汀的秒值,以另一种解决方案0通过astype

print (df.groupby([df.index.values.astype('<M8[m]')])['value'].mean())
2015-05-03 00:00:00    60.6
2015-05-03 00:01:00    60.6
2015-05-03 00:02:00    60.2
2015-05-03 00:03:00    59.2
Name: value, dtype: float64


文章来源: Group index by minute and compute average