Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 如何按天聚合数据帧_Pandas_Dataframe_Aggregate_Aggregate Functions_Pandas Groupby - Fatal编程技术网

Pandas 如何按天聚合数据帧

Pandas 如何按天聚合数据帧,pandas,dataframe,aggregate,aggregate-functions,pandas-groupby,Pandas,Dataframe,Aggregate,Aggregate Functions,Pandas Groupby,我得到了以下数据帧: time, value 2018-08-02 09:19:37, 2 2018-08-02 09:19:47, 3 2018-08-02 09:19:57, 6 ...... 2018-08-03 04:49:27, 2 2018-08-03 04:49:37, 4 2018-08-03 04:49:47, 5 我希望构建一个输出数据帧,如下所示: time, value 2018-08-02 11:59:59,

我得到了以下数据帧:

time,                value
2018-08-02 09:19:37, 2
2018-08-02 09:19:47, 3
2018-08-02 09:19:57, 6
......
2018-08-03 04:49:27, 2
2018-08-03 04:49:37, 4
2018-08-03 04:49:47, 5
我希望构建一个输出数据帧,如下所示:

time,                value
2018-08-02 11:59:59, AVG(2+3+6+..)
2018-08-03 11:59:59, AVG(2+4+5+..)
如果您有任何帮助,我们将不胜感激。非常感谢。

IIUC,使用:

m=df.groupby(df.time.dt.date).value.mean().reset_index()
m.time=pd.to_datetime(m.time.astype(str)+' 11:59:59')
print(m)

                 time     value
0 2018-08-02 11:59:59  3.666667
1 2018-08-03 11:59:59  3.666667
注意:如果数据类型为字符串,请在上述代码之前先使用以下内容:


您可以使用
重采样

>>>df['time'] = df['time'].astype('datetime64[ns]')    
>>>df.resample('D', on='time').mean()
>>>        
time         value
2018-08-02  3.666667
2018-08-03  3.666667

df.groupby(df.time.dt.date).value.mean()
?我试过了,但是我得到了pandas.core.base.DataError:没有数字类型来聚合更改值的数据类型,
df.value=df.value.astype(int)
,然后再试一次,检查我的答案以获得详细解释非常感谢。在我投票之前。请您确认您的sol平均值为00:00:00到11:59:59?当然可以。我会做的。请确认您的sol平均了从00:00:00到11:59:59的所有值这一事实?@Carloalloca此解决方案按日期分组,并将
11:59:59
作为字符串添加到每个输出中。然后转换为<代码>日期时间>代码>,因此在<代码>时间> /COD> COL中的每一行将具有<代码> 11:59:59 ,因此它自动地考虑到同一日期内的所有值。谢谢。@Carloalloca是的。:)仅按日期分组,并将一天中的最长时间附加到时间列
>>>df['time'] = df['time'].astype('datetime64[ns]')    
>>>df.resample('D', on='time').mean()
>>>        
time         value
2018-08-02  3.666667
2018-08-03  3.666667