计算每N分钟的平均值

时间:2017-02-05 13:29:22

标签: python-3.x csv average

我的日志文件(Logfile.csv)如下所示:

2016-05-17 08:25    13.9    41.6
2016-05-17 08:26    13.9    41.6
2016-05-17 08:27    13.9    41.6
2016-05-17 08:28    13.9    41.7
2016-05-17 08:29    13.9    41.6
2016-05-17 08:30    13.9    41.6

第一行是时间戳 第二排是温度 第3行是湿度

如何迭代线条,以便我可以计算每N分钟的温度和湿度的平均值。

f = open("Logfile.csv","r")
f_reader = csv.reader(f,delimiter="\t",dialect='excel-tab')
for row in f_reader:
        stringRowDate = row[0]
        Date = datetime.datetime.strptime(stringRowDate,"%Y-%m-%d %H:%M")
        floatTemp = float(row[1])
        floatHum = float(row[2])
        ... group N minutes and calculate mean values for floatTemp and floatHum

我知道大熊猫可能是一个不错的选择,但我更喜欢与csv相关的方法

这是我最终的结果:

import csv
from datetime import timedelta
import datetime

last_time = None
temperatures = []
humidities = []

f = open("DataLogger.csv", "r+")
f_reader = csv.reader(f,delimiter="\t",dialect='excel-tab')
for row in f_reader:
    current_time = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M")
    if last_time is None:
        last_time = current_time
    temperatures.append(float(row[1]))
    humidities.append(float(row[2]))
    if (current_time - last_time) > datetime.timedelta(minutes=10):
        with open('/DataLoggerAVE.csv', 'a') as f:
        x = csv.writer(f,dialect='excel-tab')
        avetemp=temperature=sum(temperatures) / len(temperatures)
        avehumidity=sum(humidities) / len(humidities)
        x.writerow([current_time, avetemp, avehumidity])
        last_time = current_time
        temperatures = []
        humidities = []

我得到了每个时间间隔的平均值,我用时间值来定义湿度和温度 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

嗯..使用timedelta来比较时间和列表以保持值。然后像这样计算平均值:sum(values) / len(values)

以下是一些例子:

但请记住,此方法不处理剩余数据,并期望输入按日期排序。

import csv
import datetime

f_reader = csv.reader('''
2016-05-17 08:25\t13.9\t41.6
2016-05-17 08:26\t13.9\t41.6
2016-05-17 08:27\t13.9\t41.6
2016-05-17 08:28\t13.9\t41.7
2016-05-17 08:29\t13.9\t41.6
2016-05-17 08:30\t13.9\t41.6
2016-05-17 08:31\t13.8\t41.5
2016-05-17 08:32\t13.7\t41.6
2016-05-17 08:33\t13.9\t41.6
2016-05-17 08:34\t13.9\t41.7
2016-05-17 08:35\t13.9\t41.6
2016-05-17 08:40\t13.9\t41.6
2016-05-17 08:34\t13.9\t41.8
    '''.strip().splitlines(), delimiter='\t', dialect='excel-tab')

last_time = None  # keep track of the time
temperatures = []
humidities = []

for row in f_reader:
    current_time = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M")
    print(current_time)

    if last_time is None:
        last_time = current_time

    temperatures.append(float(row[1]))
    humidities.append(float(row[2]))

    if (current_time - last_time) > datetime.timedelta(minutes=3):
        print('''
averages from '{last}' to '{curr}'
    temperature:    {temperature:06.3f}
    humidity:       {humidity:06.3f}
        '''.format(
            last=last_time, curr=current_time,
            temperature=sum(temperatures) / len(temperatures),
            humidity=sum(humidities) / len(humidities),
        ).lstrip())

        last_time = current_time
        temperatures = []
        humidities = []