平均下行平均的D列

2024-06-06 01:15:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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

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

^{pr2}$

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


Tags: columns数据inforcountcolfloatxls
3条回答

我会考虑使用numpy。我不知道如何读入xls文件,但似乎有一些包提供了这个功能。我会这样做:

import numpy as np

with open("test2.txt") as f:
    for row in f:
        # row is a string, split on tabs, but ignore the values that
        # don't go into the average.  If you need to keep those you 
        # might want to look into genfromtxt and defining special datatypes
        data = (np.array(row.split('\t')[2:23])).astype(np.float)
        # split the data array into 7 separate arrays (3 columns each) and average on those
        avg = np.mean(np.array_split(data,7))
        print avg

我不确定上面的平均值是否正是你想要的。您可能需要保存较小的数组(smallArrays = np.array_split(data,7)),然后迭代这些数组,计算平均值。在

即使这不是你想要的,我还是建议你去看看numpy。我发现它真的很容易使用,而且当你想做的计算的时候非常有用。在

您可以使用decimal模块来显示确切的数字。在

from decimal import *
getcontext().prec = 6 # sets the precision to 6

请注意,使用浮点表示:

^{pr2}$

这意味着您可能需要将精度设置为更高的值,以获得6位小数。。。 例如:

from decimal import *
getcontext().prec = 28
print("{0:.6f}".format(Decimal(100) / Decimal(7))) # 14.285714

为了完整地回答你的问题,你能解释一下你要找的平均数是多少吗?所有(21)列的平均值?你能帮我做些测试吗_数据.xls?在

我不确定我是否理解您希望在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

您可以使用下面的代码更简洁地执行相同的操作:

^{pr2}$

相关问题 更多 >