2

我有一个 DataFrame 对象,它有几个这样的列:

+--------+---------------------+-------+-------+-------+
|        |        Date         | temp1 | temp2 | temp3 |
+--------+---------------------+-------+-------+-------+
|  17687 | 2013-10-21 00:04:47 | 14.50 | 13.55 | 43.06 |
|  48117 | 2013-10-21 00:18:07 | 14.64 | 13.42 | 37.48 |
|  76509 | 2013-10-21 00:33:51 | 14.32 | 13.55 | 27.26 |
| 102769 | 2013-10-21 00:48:24 | 14.38 | 13.30 | 37.10 |
| 133862 | 2013-10-21 01:04:36 | 14.77 | 13.11 | 28.25 |
| 162882 | 2013-10-21 01:18:14 | 14.50 | 13.98 | 39.71 |
| 191902 | 2013-10-21 01:34:11 | 14.39 | 13.08 | 28.69 |
| 220922 | 2013-10-21 01:48:38 | 14.31 | 13.00 | 43.56 |
| 249942 | 2013-10-21 02:04:26 | 14.10 | 13.94 | 39.79 |
| 278962 | 2013-10-21 02:18:13 | 14.01 | 13.55 | 23.46 |
| 307982 | 2013-10-21 02:34:04 | 14.00 | 13.21 | 44.94 |
| 337002 | 2013-10-21 02:48:27 | 14.81 | 13.38 | 29.44 |
+--------+---------------------+-------+-------+-------+
...
+--------+---------------------+-------+-------+-------+
|  15531 | 2013-10-22 00:05:20 | 14.84 | 13.07 | 30.25 |
|  44149 | 2013-10-22 00:18:11 | 14.35 | 13.22 | 51.02 |
| 102685 | 2013-10-22 00:49:34 | 14.46 | 13.98 | 24.17 |
| 127960 | 2013-10-22 01:04:02 | 14.07 | 13.49 | 30.74 |
| 186892 | 2013-10-22 01:34:14 | 14.75 | 13.01 | 45.77 |
| 214754 | 2013-10-22 01:48:17 | 14.35 | 13.03 | 40.75 |
| 240236 | 2013-10-22 02:02:39 | 14.31 | 13.28 | 34.88 |
| 507942 | 2013-10-21 02:34:04 | 14.87 | 13.62 | 50.16 |
| 111987 | 2013-10-21 02:48:27 | 14.74 | 13.63 | 51.36 |
+--------+---------------------+-------+-------+-------+

问题是在相同的时间间隔(例如 - 15 分钟)内找到一段时间(例如 2 天)内 temp1、temp2 和 temp3 的平均值。有两个问题:(1)遗漏了一些行;(2) 温度的测量时间略有不同(整个数据集为 +- 3 分钟,在该特定示例中为 2 分钟)。

截至目前,我的解决方案基于 2 个步骤。首先,找到一天中的最大间隔数(查看基数中的所有天数)。创建一个具有相应行数的新 DataFrame 对象。其次,如果当前日期在 3 分钟内,则遍历数据集并添加值以对应行。不幸的是,由于迭代性质,它有点慢。我正在尝试找到一种更快的方法。

有什么建议么?

谢谢

PS很高兴看到这样的结果(第一列是一些平均时间):

+---------+-------+-------+-------+
|  Time   | temp1 | temp2 | temp3 |
+---------+-------+-------+-------+
| 0:05:00 | 14.67 | 13.31 | 36.66 |
| 0:18:00 | 14.50 | 13.32 | 44.25 |
| 0:34:00 | 14.32 | 13.55 | 27.26 |
| 0:49:00 | 14.42 | 13.64 | 30.64 |
| 1:04:00 | 14.42 | 13.30 | 29.50 |
| 1:18:00 | 14.50 | 13.98 | 39.71 |
| 1:34:00 | 14.57 | 13.05 | 37.23 |
| 1:48:00 | 14.33 | 13.02 | 42.16 |
| 2:03:00 | 14.21 | 13.61 | 37.34 |
| 2:18:00 | 14.01 | 13.55 | 23.46 |
| 2:34:00 | 14.44 | 13.42 | 47.55 |
| 2:48:00 | 14.78 | 13.51 | 40.40 |
+---------+-------+-------+-------+
4

1 回答 1

1

以下是否解决了您的任务?

import datetime
from collections import defaultdict

def avg(lst):
    return sum(lst)/len(lst)

定义一些样本数据

def s2dt(s):
    fmt = '%Y-%m-%d %H:%M:%S'
    return datetime.datetime.strptime(s, fmt)

data = [(s2dt('2013-10-21 00:04:47'), 14.50, 13.55, 43.06),
        (s2dt('2013-10-21 00:18:07'), 14.64, 13.42, 37.48),
        (s2dt('2013-10-22 00:05:20'), 14.84, 13.07, 30.25),
        (s2dt('2013-10-22 00:18:11'), 14.35, 13.22, 51.02)]

定义一个按时间段对时间进行分组的函数

def coarse(dt, granularity):
    residue = dt.minute % granularity
    if residue:
        residue = granularity-residue
    dt = dt + datetime.timedelta(minutes=residue, seconds=-dt.second,
                    microseconds=-dt.microsecond)
    t = dt.time()
    return t

按间隔分组数据

groupings = defaultdict(list)
for dt, t1, t2, t3 in data:
    groupings[coarse(dt, 15)].append([t1, t2, t3])

计算平均值

averages = dict((k, map(avg, zip(*v))) for k, v in groupings.iteritems())

并得到

>>> for ct, values in sorted(averages.iteritems()):
...   print ct, ', '.join(map(lambda x: '%.2f' % x, values))
00:15:00 14.67, 13.31, 36.66
00:30:00 14.50, 13.32, 44.25
于 2013-10-25T07:47:45.413 回答