Python 按分钟分组索引并计算平均值

Python 按分钟分组索引并计算平均值,python,pandas,average,minute,pandas-groupby,Python,Pandas,Average,Minute,Pandas Groupby,因此,我有一个名为“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

因此,我有一个名为“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
进入这个数据帧

                        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()


但这似乎不起作用。有什么想法吗?

您需要首先将索引转换为:

另一个解决方案是通过
astype
将索引中的秒值设置为
0

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

print(df.groupby([df.index.values.astype('对我来说,它工作得很好。你有错误吗?df.index.resample('1Min')。mean()给出了错误属性error:'DatetimeIndex'对象没有属性'resample'和df['value'])。resample('1Min')。mean()没有给出错误但没有给出期望的结果,没有任何变化我没有得到平均值,秒数仍然在那里我准备好了df.index=df.index.to_datetime()在我的代码中,这不会转换为datetimeindex吗?你是否尝试
df.index=pd.to_datetime(df.index)
?好的,那么我真正想要的代码是df=df['value'].resample('1Min')。mean(),谢谢你,将在4分钟内接受你的答案!很高兴能帮助你。
df.resample('1Min')['value'].mean()
df['value']相同。resample('1Min')。mean()
,我把它添加到答案中。@ak3191-我认为绘制它是没有必要的,使用
s=df.groupby([df.index.values.astype')