从文本python计算多个平均值

时间:2016-03-23 20:27:43

标签: python python-3.x pandas

我是python的新手,我想从文本文件中获得多个平均值。例如:

  • 63,1,1,145,233,1,2,150,0,2.3,3,0,6,0
  • 67,1,4,160,286,0,2,10,1,1.5,2,3,3,2
  • 67,1,4,120,229,0,2,129,1,2.6,2,2,7,1
  • 37,1,3,130,250,0,0,187,0,3.5,3,0,3,0
  • 41,0,2,130,204,0,2,172,0,1.4,1,0,3,0
  • 56,1,2,120,236,0,0,178,0,0.8,1,0,3,0

    就像我只想获得像1,4,5这样的某些列的平均值。

    import pandas as pd
    df = pd.read_csv('HDPV.txt', "rb")
    columns = f.readline().strip().split(" ")
    numRows = 0
    sums = [0] * len(columns)
    for line in f:
    if not line.strip():
    continue
    values = line.split(" ")
    for i in xrange(len(values)):
        sums[i] += int(values[i])
        numRows += 1
        for index, summedRowValue in enumerate(sums):
            print ('average age:'columns[0],summedRowValue / numRows)
            print ('average chol:'columns[3],summedRowValue / numRows)
            print ('bp:'columns[4],summedRowValue / numRows)
    

1 个答案:

答案 0 :(得分:1)

我不使用pandas,但使用python list comprehension可以很容易地解决这个问题:

import csv

with open('text.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    rows = [[value.strip() for value in row] for row in reader]

    average_age_list = [float(row[0]) for row in rows]
    print ('average age: ' + str(sum(average_age_list) / float(len(average_age_list))))

    average_chol_list = [float(row[3]) for row in rows]
    print ('average chol: ' + str(sum(average_chol_list) / float(len(average_chol_list))))

    bp_list = [float(row[4]) for row in rows]
    print ('average age: ' + str(sum(bp_list) / float(len(bp_list))))

请参阅https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions