在白天和晚上对数据帧重新采样



我有一个数据帧,看起来像:

column
2015-01-01 00:00:00    27.38
2015-01-01 01:00:00    23.37
2015-01-01 02:00:00    19.33
2015-01-01 03:00:00    17.66
2015-01-01 04:00:00    17.53
2015-01-01 05:00:00    18.07
2015-01-01 06:00:00    25.23
2015-01-01 07:00:00    26.80
2015-01-01 08:00:00    26.97
2015-01-01 09:00:00    26.29

以此类推,从 2015-01-01 00:00 到 2017-12-31 23:00 每小时

我想对此进行重新采样,以便我取"夜间时间"的平均值和"白天时间"的平均值。我将晚上 18:00 到 07:00 和白天定义为 07:00 到 18:00。从而产生类似

column
2015-01-01 07:00:00    x    (This would be the mean of the values from 2015-01-01 00:00:00 to 2015-01-01 07:00:00)
2015-01-01 18:00:00    x    (This would be the mean of the values from 2015-01-01 07:00:00 to 2015-01-01 18:00:00)
2015-01-02 07:00:00    x    (This would be the mean of the values from 2015-01-01 18:00:00 to 2015-01-02 07:00:00)
2015-01-02 18:00:00    x    (This would be the mean of the values from 2015-01-02 07:00:00 to 2015-01-02 18:00:00)
2015-01-03 07:00:00    x    (This would be the mean of the values from 2015-01-02 18:00:00 to 2015-01-03 07:00:00)
2015-01-03 18:00:00    x    (This would be the mean of the values from 2015-01-03 07:00:00 to 2015-01-03 18:00:00)

我希望清楚我在问什么。如果我要解释什么,请告诉我。

一个简单的解决方法如下:

df[0] = df[0].apply(lambda x: x.split()[0]+' day' if int(x[-8:-6])<=7 else x.split()[0]+' night')
df.groupby(0)[1].mean()

基本上,我将hh:mm:ss的时间替换为daynight。这将使我可以轻松地对它们进行分组并执行所需的任何操作。

相关内容

最新更新