pandas Python:每日平均值函数

kgsdhlau  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(150)

我正在尝试创建一个函数,该函数返回一个列表/数组,其中包含来自3个csv文件之一的变量的每日平均值
每个csv文件都类似于:

date, time, variable1, variable2, variable3
2021-01-01,01:00:00,1.43738,25.838,22.453
2021-01-01,02:00:00,2.08652,21.028,19.099
2021-01-01,03:00:00,1.39101,23.18,20.925
2021-01-01,04:00:00,0.76506,22.053,19.974

该日期包含2021年全年,增量为1小时

def daily_average(data, station, variable):

该函数有3个参数:数据站:3个csv文件变量之一:变量1或2或3
可以使用datetime、calendar和numpy等库,也可以使用Pandas

wdebmtf2

wdebmtf21#

嗯。首先-试着在问问题之前自己做。这会帮助你学习东西。
但现在你的问题。

csv_lines_test = [
"date, time, variable1, variable2, variable3\n",
"2021-01-01,01:00:00,1.43738,25.838,22.453\n",
"2021-01-01,02:00:00,2.08652,21.028,19.099\n",
"2021-01-01,03:00:00,1.39101,23.18,20.925\n",
"2021-01-01,04:00:00,0.76506,22.053,19.974\n",
]

import datetime as dt

def daily_average(csv_lines, date, variable_num):
    # variable_num should be 1-3
    avg_arr = []
    # Read csv file line by line.
    for i, line in enumerate(csv_lines):
        if i == 0:
            # Skip headers
            continue
        line = line.rstrip()
        values = line.split(',')
        date_csv = dt.datetime.strptime(values[0], "%Y-%m-%d").date()
        val_arr = [float(val) for val in values[2:]]
        if date == date_csv:
            avg_arr.append(val_arr[variable_num-1])
    return sum(avg_arr) / len(avg_arr)

avg = daily_average(csv_lines_test, dt.date(2021, 1, 1), 1)
print(avg)

如果要直接从csv文件读取数据:

with open("csv_file_path.csv", 'r') as f:
    data = [line for line in f]
    avg = daily_average(data, dt.date(2021, 1, 1), 1)
    print(avg)

相关问题