Python - Calculate average for every column in a csv file

后端 未结 6 982
执笔经年
执笔经年 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:19

    This definitely worked for me!

    import numpy as np
    import csv
    
    readdata = csv.reader(open('C:\\...\\your_file_name.csv', 'r'))
    data = []
    
    for row in readdata:
      data.append(row)
    
    #incase you have a header/title in the first row of your csv file, do the next line else skip it
    data.pop(0) 
    
    q1 = []  
    
    for i in range(len(data)):
      q1.append(int(data[i][your_column_number]))
    
    print ('Mean of your_column_number :            ', (np.mean(q1)))
    

提交回复
热议问题