Skip to main content
 首页 » 编程设计

python之对一列平均数据进行平均

2024年11月01日12davidwang456

我正在用 python 为一个必须完成一些事情的项目编写代码; 1)逐列从xls文件中读入数据 2)以三组为一组对每一行的列进行平均 3) 然后平均结果列

我已经完成了 1 和 2,但似乎不太可能得到 3,我认为我遇到的很多麻烦都源于我使用的是 float ,但我需要将数字保留到小数点后 6 位。感谢任何帮助和耐心,我是 python 的新手

v = open("Pt_2_Test_Data.xls", 'wb') #created file to write output to 
w = open("test2.xls") 
 
count = 0 
 
for row in w: #read in file 
    for line in w: 
        columns = line.split("\t") #split up into columns 
        date = columns[0] 
        time = columns[1] 
        a = columns[2] 
        b = columns[3] 
        c = columns[4] 
        d = columns[5] 
        e = columns[6] 
        f = columns[7] 
        g = columns[8] 
        h = columns[9] 
        i = columns[10] 
        j = columns[11] 
        k = columns[12] 
        l = columns[13] 
        m = columns[14] 
        n = columns[15] 
        o = columns[16] 
        p = columns[17] 
        q = columns[18] 
        r = columns[19] 
        s = columns[20] 
        t = columns[21] 
        u = columns[22] 
        LZA = columns[23] 
        SZA = columns[24] 
        LAM = columns[25] 
 
        count += 1 
 
        A = 0 
        if count != 0:  # gets rid of column tiles 
            filter1 = ((float(a) + float(b) + float(c))/3) 
            filter1 = ("%.6f" %A) 
            filter2 =  (float(d) + float(e) + float(f))/3 
            filter2 = ("%.6f" %filter2) 
            filter3 =  (float(g) + float(h) + float(i))/3 
            filter3 = ("%.6f" %filter3) 
            filter4 =  (float(j) + float(k) + float(l))/3 
            filter4 = ("%.6f" %filter4) 
            filter5 =  (float(m) + float(n) + float(o))/3 
            filter5 = ("%.6f" %filter5) 
            filter6 =  (float(p) + float(q) + float(r))/3 
            filter6 = ("%.6f" %filter6) 
            filter7 =  (float(s) + float(t) + float(u))/3 
            filter7 = ("%.6f" %filter7) 
            A = [filter1, filter2, filter3, filter4, filter5, filter6, filter7] 
            A = ",".join(str(x) for x in A).join('[]') 
 
            print A 
            avg = [float(sum(col))/float(len(col)) for col in zip(*A)] 
            print avg 

我也试过像这样格式化数据:

            A = ('{0}    {1}    {2}     {3}    {4}    {5}    {6}    {7}    {8}'.format(date, time, float(filter1), float(filter2), float(filter3), float(filter4), float(filter5), float(filter6), float(filter7))+'\n') # average of triplets 
            print A 

我想我可以访问每一列的值并通过像使用字典时那样调用它们来对它们执行必要的数学运算,但是这是不成功的:它似乎将数据识别为一行(所以试图通过 [0] 访问任何列是越界的)或通过单个字符访问任何列,而不是作为数字列表。这与使用 float 函数有关吗?

请您参考如下方法:

我不确定我是否理解您想在 3) 中对哪些列进行平均,但也许这会满足您的要求:

with open("test2.xls") as w: 
    w.next()  # skip over header row 
    for row in w: 
        (date, time, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, 
         u, LZA, SZA, LAM) = row.split("\t")  # split columns into fields 
 
        A = [(float(a) + float(b) + float(c))/3, 
             (float(d) + float(e) + float(f))/3, 
             (float(g) + float(h) + float(i))/3, 
             (float(j) + float(k) + float(l))/3, 
             (float(m) + float(n) + float(o))/3, 
             (float(p) + float(q) + float(r))/3, 
             (float(s) + float(t) + float(u))/3] 
        print ('['+ ', '.join(['{:.6f}']*len(A)) + ']').format(*A) 
        avg = sum(A)/len(A) 
        print avg 

您可以使用如下代码更简洁地完成同样的事情:

avg = lambda nums: sum(nums)/float(len(nums)) 
 
with open("test2.xls") as w: 
    w.next()  # skip over header row 
    for row in w: 
        cols = row.split("\t")  # split into columns 
        # then split that into fields 
        date, time, values, LZA, SZA, LAM = (cols[0], cols[1], 
                                             map(float, cols[2:23]),  
                                             cols[23], cols[24], cols[25]) 
        A = [avg(values[i:i+3]) for i in xrange(0, 21, 3)] 
        print ('['+ ', '.join(['{:.6f}']*len(A)) + ']').format(*A) 
        print avg(A)