Python 如果CSV文件中也有日期列,如何计算CSV中某列的月平均值

Python 如果CSV文件中也有日期列,如何计算CSV中某列的月平均值,python,csv,Python,Csv,这是CSV/Excel文件的外观: `2008-09-19 461 462.07 443.28 449.15 10006000 449.15 2008-09-18 422.64 439.18 410.5 439.08 8589400 439.08 2008-09-17 438.48 439.14 413.44 414.49 9126900 414.49 2008-09-16 425.96 449.28 425.49 442.93 6990700 442

这是CSV/Excel文件的外观:

`2008-09-19 461 462.07  443.28  449.15  10006000    449.15
2008-09-18  422.64  439.18  410.5   439.08  8589400 439.08
2008-09-17  438.48  439.14  413.44  414.49  9126900 414.49
2008-09-16  425.96  449.28  425.49  442.93  6990700 442.93
2008-09-15  424 441.97  423.71  433.86  6567400 433.86
2008-09-12  430.21  441.99  429 437.66  6028000 437.66
2008-09-11  408.35  435.09  406.38  433.75  6471400 433.75
2008-09-10  424.47  424.48  409.68  414.16  6226800 414.16
2008-09-09  423.17  432.38  415 418.66  7229600 418.66
2008-09-08  452.02  452.94  417.55  419.95  9017900 419.95
2008-09-05  445.49  452.46  440.08  444.25  4534300 444.25
2008-09-04  460 463.24  449.4   450.26  4848500 450.26
2008-09-03  468.73  474.29  459.58  464.41  4314600 464.41
2008-09-02  476.77  482.18  461.42  465.25  6111500 465.25
2008-08-29  469.75  471.01  462.33  463.29  3848200 463.29
2008-08-28  472.49  476.45  470.33  473.78  3029700 473.78
`
我需要计算最后一列的月平均值,并将其存储在列表中

到目前为止,我所做的真的很糟糕,因为我已经在一起拼凑垃圾代码好几天了,现在正在努力使它正确。事实上,我知道有更有效(和功能)的方法来做这件事。对于任何感兴趣的人,以下是我当前的功能失调代码:

def get_monthly_averages (data_list):
    date_list = []
    monthly_average_list = []
    current_date = ''
    nums = []
    count = 0
    total = 0
    average = 0
    for index, row in enumerate(data_list):
        data_list[index] = row.split(",")
    for index, row in enumerate(data_list):
        if index > 0:
            date_list.append(row[0])
            data_list[index] = [float(i) for i in row if row.index(i) > 0]
    for index, row in enumerate(data_list):
        if index == 1:
            current_date = str(date_list[index-1])
            current_date = current_date[:-3]
            count += 1
            nums = row[5:6]

        elif index > 1 and current_date in date_list[index - 1]:
            nums = row[5:6]




            monthly_average_list.append(average)

        elif index > 1 and current_date not in date_list[index - 1]:
            current_date = str(date_list[index-1])
            current_date = current_date[:-3]
            nums = row[5:6]
            total = 0




    print(monthly_average_list[0])
    print(current_date)
    print(date_list[0])
    print(data_list[1])    

    return monthly_average_list

如果我正确地理解了你想要的结果,是的,我认为你想得太多了。如果希望得到某列的逐月平均值,则只需找到该列中的所有单元格,将其按月分组,然后对其进行平均:

from collections import defaultdict

def get_col_avg_by_month(data, colnumber):
    result = defaultdict(lambda: [])
    for row in (row.split() for row in data):
        date = year, month = row[0].split('-')[:2]
        result[tuple(date)].append(float(row[colnumber]))
    return {date: avg(data) for date, data in result.items()}
在这里,对于每一行,我们获取月份(我们与年份配对以区分2017年9月和2016年9月),并将目标列的单元格附加到该月份的结果列表中(我们使用
defaultdict
,以便我们第一次输入新月份时,我们从一个空列表开始)。最后,在返回之前,我们重新计算结果dict以保存数据集的平均值,而不是数据集本身(
avg=lambda lst:sum(lst)/len(lst)