沿着具有一些缺失行的DataFrame列进行平均

时间:2013-10-24 13:21:32

标签: python pandas

我有一个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分钟)。

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

有什么建议吗?

由于

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

+---------+-------+-------+-------+
|  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 |
+---------+-------+-------+-------+

1 个答案:

答案 0 :(得分: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