使用python计算excel文件的mean和std

时间:2015-10-09 09:07:48

标签: python

我真的很擅长python。 我在excel文件上有三个col和大约7000~8000行。在将数据导入python之后,如何计算每个col的平均值和std。

import xlrd
file_location = "C:/Users/Roy/Desktop/table.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet=workbook.sheet_by_index(0)

3 个答案:

答案 0 :(得分:4)

您可以使用pandas执行此操作:

import pandas as pd
df = pd.read_excel("C:/Users/Roy/Desktop/table.xlsx")
print(df.mean())
print(df.std())

答案 1 :(得分:0)

您应该查看functions in the Sheet class of xlrd

如果你使用的是python 3.4+,那么standard library module for statistics会产生均值和标准偏差。请查看此处的文档,并确定您是需要stdev还是pstdev。否则为here is a previous answer提供平均值和标准差操作的示例代码。

from statistics import mean, stdev
my_stats = []
for i in range(sheet.ncols):
    m = mean(c.value for c in sheet.col(i))
    s = stdev(c.value for c in sheet.col(i))
    my_stats.append((m,s))

答案 2 :(得分:0)

您可以使用 numpy 库来计算Excel工作表中大量值的标准。 请参阅以下代码:

导入csv

import numpy as np
import math

#read csv file and convert it in to an array
csv= np.genfromtxt
csv = np.genfromtxt('your excel sheet name.csv',delimiter=",")
N=len(csv[:])
#print N(N is number of raws in the sheet)
#X is x bar which is the mean of each column
X = np.mean(csv,axis=0)
sum = 0

for j in range(len (X[:])):
    for i in range(N):
        val= csv[i,j]-X[j]
        squre = val**2
        sum= sum+squre
    final = math.sqrt(sum/(N-1))
    print final

此代码的输出将显示每列的stds数组。