Python - Calculate average for every column in a csv file

后端 未结 6 983
执笔经年
执笔经年 2020-12-10 20:02

I\'m new in Python and I\'m trying to get the average of every (column or row) of a csv file for then select the values that are higher than the double of the average of its

6条回答
  •  孤城傲影
    2020-12-10 20:11

    I hope this helps you out......Some help....here is what I would do - which is use numpy:

        # ==========================
        import numpy as np
        import csv as csv
    
        #  Assume that you have 2 columns and a header-row: The Columns are (1) 
        #  question # ...1; (2) question 2
        # ========================================
    
        readdata = csv.reader(open('filename.csv', 'r'))  #this is the file you 
        # ....will write your original file to....============
        data = []
        for row in readdata:
        data.append(row)
        Header = data[0]
        data.pop(0)
        q1 = []
        q2 = []
        # ========================================
    
        for i in range(len(data)):
            q1.append(int(data[i][1]))
            q2.append(int(data[i][2]))
        # ========================================
        # ========================================
        # === Means/Variance - Work-up Section ===
        # ========================================
        print ('Mean - Question-1:            ', (np.mean(q1)))
        print ('Variance,Question-1:          ', (np.var(q1)))
        print ('==============================================')
        print ('Mean - Question-2:            ', (np.mean(q2)))
        print ('Variance,Question-2:          ', (np.var(q2)))
    

提交回复
热议问题