【问题标题】:Getting averages from monthly data从月度数据中获取平均值
【发布时间】:2013-10-27 16:57:17
【问题描述】:

我有一个包含大约 2000 行太阳黑子数据的文件。我需要每个月找出它的平均值并将其写入一个新文件。我如何对月份进行分组以便获得平均值?我已经阅读了一些建议使用 panda 的帖子,但是由于我们还没有在课堂上到达那里,所以我宁愿在没有完全掌握它的作用的情况下不使用它。

到目前为止,我的代码将年月日分开。如何将月份分组以找到平均太阳黑子?

到目前为止,这是我的代码:

def OpenFile(File):
    outfile = open ("Monthlytemp.txt","w")

    try:
        Lines= open(File).readlines()
    except IOError:
        Lines=[]
    for line in Lines:
        Dates = line.split()
        Year= str(Dates[0][0:4])
        Month = str(Dates[0][4:6])
        Date = str(Dates [0][6:8])
        Spots = int(Dates [2])
        if Spots == 999:
            Spots= ''
        Spots = str(Spots)
        Data = [Year, Month, Date, Spots, '\n']
        Data = ' '.join(Data)
        outfile.write(str(Data))
        #print (Data)
    outfile.close()
    return Data

【问题讨论】:

    标签: python python-3.x average


    【解决方案1】:

    您可以使用字典。

    def OpenFile(File):
        outfile = open ("Monthlytemp.txt","w")
    
        # stores (year, month): spots
        spots_by_month = dict()
    
        try:
            Lines= open(File).readlines()
        except IOError:
            Lines=[]
        for line in Lines:
            Dates = line.split()
            Year= str(Dates[0][0:4])
            Month = str(Dates[0][4:6])
            Date = str(Dates [0][6:8])
            Spots = int(Dates [2])
    
            # Not sure if this should be here, might want to place it
            # in an else clause after that if clause
            spots_by_month.get((Year, Month), []).append(Spots)
    
            if Spots == 999:
                Spots= ''
    
            Spots = str(Spots)
    
            Data = [Year, Month, Date, Spots, '\n']
            Data = ' '.join(Data)
            outfile.write(str(Data))
            #print (Data)
    
        # Getting averages as a dictionary
        averages = {
            date:sum(spots_list) / len(spots_list)
            for date, spots_list in spots_by_month.items()
        }
        print(averages)
    
        # Alternatively getting the averages as a sorted list
        averages = [
            (date, sum(spots_list) / len(spots_list))
            for date, spots_list in spots_by_month.items()
        ]
        averages.sort()
        print(averages)
    
        outfile.close()
        return Data
    

    【讨论】:

    • 我尝试将打印和输出文件替换为字典,但输出为 {}{}{}{}{}{}{}{}{}{}{}{}{} {}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{} {}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
    【解决方案2】:

    一种可能的解决方案(对您的方法进行最小的更改):

    def WriteAvg(outfile, year, month, avg):
        Data = [year, month, avg, '\n']
        Data = ' '.join(Data)
        outfile.write(str(Data))
    
    def OpenFile(File):
        outfile = open ("Monthlytemp.txt","w")
        PrevMonth = ""
        PrevYear = ""
        SpotSum = 0
        Days = 0
    
        try:
            Lines= open(File).readlines()
        except IOError:
            Lines=[]
        for line in Lines:
            Dates = line.split()
            Year= str(Dates[0][0:4])
            Month = str(Dates[0][4:6])
            Date = str(Dates [0][6:8])
            Spots = int(Dates [2])
            if PrevMonth != Month && PrevMonth!="":
                MonthAvg = str(SpotSum*1./Days)
                WriteAvg(outfile, PrevYear, PrevMonth, MonthAvg)
                Days = 0
                SpotSum = 0
            if Spots!= 999:
                Days +=1
                SpotSum += Spots
            PrevMonth = Month
            PrevYear = Year
        #one last time
        MonthAvg = str(SpotSum*1./Days)
        WriteAvg(outfile, PrevYear, PrevMonth, MonthAvg)
    
        outfile.close()
        return Data
    

    【讨论】:

    • “elif Spots!=999”部分是什么?我在我的程序中这样做的原因是因为 999 是几个月的数据点,但我们应该把它扔掉。这就是为什么我将它设置为等于一个空字符串
    • 我猜的太多了,所以我也把它扔掉了(所以不计算 Spots=999 的天数。哦等等……需要稍微修正一下(修正了一个月的那一刻)切换,因为该条目也必须计算在内)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2021-04-04
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多