分钟平均csv文件

时间:2011-11-10 23:42:52

标签: python

我有一个大的csv文件,每10秒记录一次日期时间和值。 csv文件如下所示:

Datetime             Data  
2008-10-01 12:00:10, 34  
2008-10-01 12:00:20, 55  
2008-10-01 12:00:30, 46  
2008-10-01 12:00:40, 33  
2008-10-01 12:00:50, 55  
2008-10-01 12:01:00, 21  
2008-10-01 12:01:10, 2  
2008-10-01 12:01:20, 34  
2008-10-01 12:01:30, 521  
2008-10-01 12:01:40, 45  
2008-10-01 12:01:50, 32  
2008-10-01 12:02:00, 34  

我想编写一个计算分钟平均值的脚本,并将其写入新的csv文件中,并提供以下输出:

Datetime             Data  
2008-10-01 12:00:00, 40.67  
2008-10-01 12:01:00, 111.33  

知道如何做到这一点以及有关我应该研究的模块或任何示例的任何建议。

2 个答案:

答案 0 :(得分:2)

在我看来,最简单的方法就是将时间视为字符串,而不是时间,并使用itertools.groupby

from csv import reader
from itertools import groupby

lines = """Datetime             Data
2008-10-01 12:00:10, 34
2008-10-01 12:00:20, 55
2008-10-01 12:00:30, 46
2008-10-01 12:00:40, 33
2008-10-01 12:00:50, 55
2008-10-01 12:01:00, 21
2008-10-01 12:01:10, 2
2008-10-01 12:01:20, 34
2008-10-01 12:01:30, 521
2008-10-01 12:01:40, 45
2008-10-01 12:01:50, 32
2008-10-01 12:02:00, 34"""

lines = iter(lines.splitlines())

# above this is just for testing, really you'd do
# with open('filename', 'rb') as lines:
# and indent the rest

next(lines)

for minute, group in groupby(reader(lines), lambda row: row[0][:16]):
    group = list(group)
    print minute, sum(float(row[1]) for row in group) / len(group)

答案 1 :(得分:1)

使用 csv.reader 解析文件和字典以对结果进行聚类。 str.rpartition 方法可以分割秒数。使用 sum len 计算平均值:

data = '''\
2008-10-01 12:00:10, 34  
2008-10-01 12:00:20, 55  
2008-10-01 12:00:30, 46  
2008-10-01 12:00:40, 33  
2008-10-01 12:00:50, 55  
2008-10-01 12:01:00, 21  
2008-10-01 12:01:10, 2  
2008-10-01 12:01:20, 34  
2008-10-01 12:01:30, 521  
2008-10-01 12:01:40, 45  
2008-10-01 12:01:50, 32  
2008-10-01 12:02:00, 34
'''.splitlines()

import csv

d = {}
for timestamp, value in csv.reader(data):
    minute, colon, second = timestamp.rpartition(':')
    if minute not in d:
        d[minute] = [float(value)]
    else:
        d[minute].append(float(value))

for minute, values in sorted(d.items()):
    avg_value = sum(values) / len(values)
    print minute + ',' + str(avg_value)