2017-05-31 83 views
3

我有一个数据帧df具有2分钟分辨率数据的几个星期:汇总时间为定义的插槽

df.dtypes

time_stamp  datetime64[ns] 
Day_name    object 
x      int64 
y     int64 

df.head

time_stamp    Day_name x y 
0 2017-05-17 14:28:35 Wednesday 100 200 
1 2017-05-17 14:30:32 Wednesday 300 400 

我要汇总指标xy,并找到他们的平均'15'minut e时期。我原本有一个时代指标,但我已将其转换为上面显示的datetime

time_stamp    Day_name x y  15_min_slot 
    0 2017-05-17 14:28:35 Wednesday 100 200 14:15 
    1 2017-05-17 14:30:32 Wednesday 300 400 14:30 

我该怎么做?

我可以通过找到时间:

df['hour'] = df['time_stamp'].dt.hour

df['minute'] = df['time_stamp'].dt.minute

然后我最终会做的是:

output = df.groupby(['15_min_slot'],as_index=False)['x'].mean()

回答

3

您可以使用Grouper,在组合与freq的论点,即:

df.groupby(pd.Grouper(key=df['time_stamp'], freq='15T')).mean() 
+0

快速的问题 - 是有可能做的样品任何'15'min期,在整个数据集?那么基本上,一天的平均时间是15分钟? – LearningSlowly

+0

然后创建一个只有小时和分钟的新列(即'df ['new_column'] = df ['time_stamp']。hour'或其变体 – Mathias711

1

让我们使用resample

首先创建一个datetimeindex为您的数据帧

df = df.set_index('time_stamp') 
df.index = pd.to_datetime(df.index,format='%Y-%m-%d %H:%M:%S') 

然后用resample15Tmean

df.resample('15T').mean() 

输出:

     x y 
time_stamp     
2017-05-17 14:15:00 100 200 
2017-05-17 14:30:00 300 400