2016-07-18 251 views
2

我有一个数据帧,时间是一个浮动相关的数据集:熊猫计算平均每小时

Time   Value 
-47.88333   90 
-46.883333  23 
-45.900000  66 
-45.883333  87 
-45.383333  43 

列从-48范围为0。我想什么做的时间是在计算平均值从-47.5到-5每半小时一班。 例如:

-47.5是所有落在-48和-47之间的所有值的平均值,-46.5是落在-47和-46之间的所有值的平均值。如果没有价值,我想继续前面的平均水平。

产生输出会看起来像:

Time   Value 
-47.5    90 
-46.5    23 
-45.5    65.33 
-44.5    65.33 
-43.5    65.33 

请问这需要是一个自定义函数,因为时间列不是DateTime对象?

+0

是什么时间价值代表什么? -46.5 =在某个时间点之前30分钟46小时? –

+0

这是正确的 – GNMO11

回答

3

您可以用GROUPBY做到这一点很容易地:

(df.groupby(df.Time.apply(lambda x: np.floor(x) + 0.5)) 
    .mean() 
    .Value 
    .reindex(np.arange(-47.5, -42.5)) 
    .ffill()) 

Time 
-47.5 90.000000 
-46.5 23.000000 
-45.5 65.333333 
-44.5 65.333333 
-43.5 65.333333 
Name: Value, dtype: float64 
2

尝试用pd.cut二进制化时间变量:

#change the bins arg to modify the size of the bins 
df.loc[:, 'TimeBin'] = pd.cut(df.Time, bins=[i for i in range (-48, 0)]) 
#groupby the time bin and take the mean: 
df[['TimeBin', 'Value']].groupby('TimeBin').mean()